New to Telerik UI for WinForms? Download free 30-day trial

Getting Started with WinForms VirtualGrid

The example bellow demonstrates how one can use RadVirtualGrid with a list which contains large amount of data. The example shows how you can use the control events to add or remove rows as well.

WinForms RadVirtualGrid Getting Started

In order to use RadVirtualGrid you should add reference to the Telerik.WinControls.GridView assembly.

Setting the form and adding data

1. Add a RadVirtualGrid to a form and set its Dock property to Fill . 2. Add the following sample class to the project.


public class CarPart
{
    public string Name { get; set; }

    public string Make { get; set; }

    public int PartID { get; set; }

    public string this[int i]
    {
        get
        {
            switch (i)
            {
                case 0:
                    return Name;
                case 1:
                    return Make;
                case 2:
                    return PartID.ToString();
                default:
                    return string.Empty;
            }
        }
    }
}

3. Now you can create the list of objects which will be used as data source. In addition you can create an array that contains the column names.


List<CarPart> data = new List<CarPart>();
private string[] columnNames = new string[]
{
    "Name",
    "Make",
    "PartId"
};

public VirtualGridGettingStarted()
{
    InitializeComponent();
    for (int i = 0; i < 1000000; i++)
    {
        data.Add(new CarPart()
        {
            Name = "Name " + i,
            Make = "Tesla",
            PartID = i
        });
    }
}

Using the virtual grid

1. To use the grid you should first specify the count of columns and rows. In addition, you should subscribe to the CellValueNeeded and CellValuePushed events which are used for populating the grid with data and updating the data source when values are changed:


radVirtualGrid1.CellValueNeeded += RadVirtualGrid1_CellValueNeeded;
radVirtualGrid1.CellValuePushed += RadVirtualGrid1_CellValuePushed;
radVirtualGrid1.ColumnCount = columnNames.Length;
radVirtualGrid1.RowCount = data.Count;

2. Now you can add the CellValueNeeded event handler. In it we will retrieve the cell value and pass it to the grid according to the current row/column index. The event is fired for the header row so you can set the header cells text as well.


private void RadVirtualGrid1_CellValueNeeded(object sender, VirtualGridCellValueNeededEventArgs e)
{
    if (e.ColumnIndex < 0)
        return;

    if (e.RowIndex == RadVirtualGrid.HeaderRowIndex)
    {
        e.Value = columnNames[e.ColumnIndex];
    }
    if (e.RowIndex >= 0 && e.RowIndex < data.Count)
    {
        e.Value = data[e.RowIndex][e.ColumnIndex];
    }
}

3. When a cell value is changed the CellValuePushed event will fire. This will allow you to update the value in the data source:


private void RadVirtualGrid1_CellValuePushed(object sender, VirtualGridCellValuePushedEventArgs e)
{
    switch (e.ColumnIndex)
    {
        case 0:
            data[e.RowIndex].Name = e.Value.ToString();
            break;
        case 1:
            data[e.RowIndex].Make = e.Value.ToString();
            break;
        case 2:
            data[e.RowIndex].PartID = Convert.ToInt32(e.Value.ToString());
            break;
        default:
            break;
    }
}

Add or remove rows

By default the end user can add or remove rows with the UI. When such operation is performed the UserAddedRow or UserDeletingRow events will fire.

The user can delete multiple rows at once.

The following example shows how you can handle the above events and properly update the data source.


private void RadVirtualGrid1_UserAddedRow(object sender, VirtualGridNewRowEventArgs e)
{
    data.Add(new CarPart()
    {
        Name = e.NewValues[0].ToString(),
        Make = e.NewValues[1].ToString(),
        PartID = Convert.ToInt32(e.NewValues[2].ToString())
    });
}
private void RadVirtualGrid1_UserDeletedRow(object sender, VirtualGridRowsEventArgs e)
{
    var indexesToRemove = e.RowIndices.ToList();

    for (int i = indexesToRemove.Count - 1; i >= 0; i--)
    {
        data.RemoveAt(indexesToRemove[i]);
    }
}

Telerik UI for WinForms Learning Resources