)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: January 3, 2016 |
Last Revised Date: December 16, 2019

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 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon