Category / Section
How to set a value into the cell in WinForms GridControl?
1 min read
Setting cellvalue
The style object holds all the information that affects the cell’s appearance. You can set the value for the cell by using the CellValue property.
Use a two-parameter indexer rowIndex, columnIndex on your Gridcontrol object to get a reference to that particular cell’s style.
C#
int rowIndex = 1, columnIndex = 2; gridControl1[rowIndex, columnIndex].CellValue = "PI"; rowIndex++; gridControl1[rowIndex, columnIndex].CellValue = 3.14159; rowIndex++; gridControl1[rowIndex, columnIndex].CellValue = DateTime.Now;
VB
Dim rowIndex As Integer = 1, columnIndex As Integer = 2 gridControl1(rowIndex, columnIndex).CellValue = "PI" rowIndex += 1 gridControl1(rowIndex, columnIndex).CellValue = 3.14159 rowIndex += 1 gridControl1(rowIndex, columnIndex).CellValue = DateTime.Now
You can also set CellValue by using QueryCellInfo event.
C#
//Hook the QueryCellInfo event in Form_Load gridControl1.QueryCellInfo += gridControl1_QueryCellInfo; void gridControl1_QueryCellInfo(object sender,GridQueryCellInfoEventArgs e) { if (e.ColIndex == 3) { if (e.RowIndex == 1) e.Style.CellValue = "Fraction"; if(e.RowIndex == 2) e.Style.CellValue = "22/7"; if(e.RowIndex == 3) e.Style.CellValue = DateTime.Now; } }
VB
'Hook the QueryCellInfo event in Form_Load Private gridControl1.QueryCellInfo += AddressOf gridControl1_QueryCellInfo Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) If e.ColIndex = 3 Then If e.RowIndex = 1 Then e.Style.CellValue = "Fraction" End If If e.RowIndex = 2 Then e.Style.CellValue = "22/7" End If If e.RowIndex = 3 Then e.Style.CellValue = DateTime.Now End If End If End Sub
Note:
You can set different data types for the CellValue since its data type is an object.
Figure 1: Setting CellValue in Gridcontrol
Samples:
C#: SetCellValue
VB: SetCellValue