Articles in this section
Category / Section

Why the cell values of virtual grid cannot be changed as like normal grid in WinForms GridControl?

2 mins read

Virtual grid

In GridControl, by default virtual grid will bind initially 10 rows and 10 columns, only those cells can be accessed initially. So, if 11th row cell is trying to access, then the ArgumentOutofRangeException will be thrown.

Solution

If you want to set the data to grid as virtual, then we can use the QueryRowCount, QueryColCount events to define the row and column count.

The values for the cells can be populated from and saved to the datasource using, the QueryCellinfo and SaveCellInfo events.

C#

// Trigger the required events
this.gridControl1.QueryCellInfo += new GridQueryCellInfoEventHandler(GridQueryCellInfo);
this.gridControl1.SaveCellInfo += new GridSaveCellInfoEventHandler(GridSaveCellInfo);
this.gridControl1.Model.QueryRowCount += gridControl1_QueryRowCount;
 
private void gridControl1_QueryRowCount(object sender, GridRowColCountEventArgs e)
{
   e.Count = this._extData.RowCount;
   e.Handled = true;
}
 
//Saves the changes back to the datasource
private void GridSaveCellInfo(object sender, GridSaveCellInfoEventArgs e)
{
   try
   {
   //Move the changes back to the external data object
   if (e.ColIndex > 0 && e.RowIndex > 0)
   {
   //Either of the next two lines will work without setting CellValueType
   this._extData(e.RowIndex - 1, e.ColIndex - 1) = int.Parse(e.Style.CellValue.ToString());
   }
   }
   catch
   {
   }
   e.Handled = true;
}
 
private void GridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
   //Sets the cell value from ExternaData
   if (e.RowIndex > 0 && e.ColIndex > 0)
   {
   e.Style.CellValue = this._extData(e.RowIndex - 1, e.ColIndex - 1);
   e.Handled = true;
   }
   //Cell values should be accessed only in events for virtual grid.
   if (e.RowIndex == 0 && e.ColIndex == 11)
   {
   object test3 = "Sep";
   object colname = e.Style;
   colname.Text = test3;
   e.Style.Text = colname.Text;
   }
}
 

 

VB

' Trigger the required events
AddHandler gridControl1.QueryCellInfo, AddressOf GridQueryCellInfo
AddHandler gridControl1.SaveCellInfo, AddressOf GridSaveCellInfo
Me.gridControl1.Model.QueryRowCount += gridControl1_QueryRowCount
 
Private Sub gridControl1_QueryRowCount(ByVal sender As Object, ByVal e As GridRowColCountEventArgs)
   e.Count = Me._extData.RowCount
   e.Handled = True
End Sub
 
'Saves the changes back to the datasource
Private Sub GridSaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs)
   Try
   'Move the changes back to the external data object
   If e.ColIndex > 0 AndAlso e.RowIndex > 0 Then
   'Either of the next two lines will work without setting CellValueType
   Me._extData(e.RowIndex - 1, e.ColIndex - 1) = Integer.Parse(e.Style.CellValue.ToString())
   End If
   Catch
   End Try
   e.Handled = True
End Sub
 
 
Private Sub GridQueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)
   'Sets the cell value from ExternaData
   If e.RowIndex > 0 AndAlso e.ColIndex > 0 Then
   e.Style.CellValue = Me._extData(e.RowIndex - 1, e.ColIndex - 1)
   e.Handled = True
   End If
   'Cell values should be accessed only in events for virtual grid.
   If e.RowIndex = 0 AndAlso e.ColIndex = 11 Then
   Dim test3 = "Sep"
   Dim colname = e.Style
   colname.Text = test3
   e.Style.Text = colname.Text
   End If
End Sub
 

 

Samples:

C#: VirtualGrid_CS

VB: VirtualGrid_VB

Reference link: https://help.syncfusion.com/windowsforms/grid-control/virtual-grid 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied