How to change the image of a cell at runtime in WinForms Grid Data?
Solution:
To change the image of a cell at runtime based on the value of the adjacent cell, you can make use of PrepareViewStyleInfo and CurrentCellCloseDropDown events in WinForms GridDataBoundGrid.
In the following code, when you are selecting the value in the combo box placed in the first row of the fourth column, the appropriate image is shown in the first row of the fifth column.
C#
this.gridDataBoundGrid1.PrepareViewStyleInfo += gridDataBoundGrid1_PrepareViewStyleInfo; this.gridDataBoundGrid1.CurrentCellCloseDropDown += gridDataBoundGrid1_CurrentCellCloseDropDown; void gridDataBoundGrid1_CurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.PopupClosedEventArgs e) { //Get the current cell value. cc = this.gridDataBoundGrid1.CurrentCell; newvalue = cc.Renderer.GetCellValue().ToString(); this.Refresh(); } void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) { if (e.RowIndex == 1 && e.ColIndex == 4) { //Set the items to the combobox. e.Style.CellType = "ComboBox"; e.Style.DataSource = items; } else if (e.RowIndex == 1 && e.ColIndex == 5) { //Set the cell type as image. e.Style.CellType = GridCellTypeName.Image; e.Style.ImageList = this.imageList; if (newvalue == "s2") e.Style.ImageIndex = 1; else if (newvalue == "s3")//New value is obtained from GridCurrentCell renderer. e.Style.ImageIndex = 2; else e.Style.ImageIndex = 0; } }
VB
Private Me.gridDataBoundGrid1.PrepareViewStyleInfo += AddressOf gridDataBoundGrid1_PrepareViewStyleInfo Private Me.gridDataBoundGrid1.CurrentCellCloseDropDown += AddressOf gridDataBoundGrid1_CurrentCellCloseDropDown Private Sub gridDataBoundGrid1_CurrentCellCloseDropDown(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.PopupClosedEventArgs) 'Get the current cell value. cc = Me.gridDataBoundGrid1.CurrentCell newvalue = cc.Renderer.GetCellValue().ToString() Me.Refresh() End Sub Private Sub gridDataBoundGrid1_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As GridPrepareViewStyleInfoEventArgs) If e.RowIndex = 1 AndAlso e.ColIndex = 4 Then 'Set the items to the combobox e.Style.CellType = "ComboBox" e.Style.DataSource = items ElseIf e.RowIndex = 1 AndAlso e.ColIndex = 5 Then 'Set the celltype as image e.Style.CellType = GridCellTypeName.Image e.Style.ImageList = Me.imageList If newvalue = "s2" Then e.Style.ImageIndex = 1 ElseIf newvalue = "s3" Then 'New value is obtained from GridCurrentCell renderer. e.Style.ImageIndex = 2 Else e.Style.ImageIndex = 0 End If End If End Sub
After applying the properties, the grid is shown as follows,
Figure 1: Output
Sample Link:
C#: Image
VB: Image
Conclusion
I hope you enjoyed learning about how to change the image of a cell at runtime in WinForms Grid Data.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms DataGrid example to understand how to create and manipulate data.
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!