Articles in this section

How to set a value into the cell in WinForms GridControl?

Setting cell value


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, columnInde,x on your Gridcontrol object to get a reference to that particular cell’s style.

 

int rowIndex = 1, columnIndex = 2;
gridControl1[rowIndex, columnIndex].CellValue = "PI";
rowIndex++;
gridControl1[rowIndex, columnIndex].CellValue = 3.14159;
rowIndex++;
gridControl1[rowIndex, columnIndex].CellValue = DateTime.Now;
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 the QueryCellInfo event.

//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;
    }
}
'Hook the QueryCellInfo event in Form_Load 
AddHandler Me.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.

 

Set the cellvalue in gridcontrol


  Figure
1: Setting CellValue in Gridcontrol

Samples:

C#: SetCellValue

VB: SetCellValue

 

Conclusion

I hope you enjoyed learning about how to set a value into the cell in WinForms GridControl.


You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.


For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.


If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

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