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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the image of a cell at runtime, based on the value of the adjacent cell?

Platform: WinForms |
Control: GridDataBoundGrid(Classic)
Tags: image, combobox

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 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,

Runtime Output

Figure 1: Output

Sample Link:

C#: Image

VB: Image

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