Articles in this section
Category / Section

How to change the color of button in ButtonEdit cell while clicking in WinForms GridControl or GridDataBoundGrid?

2 mins read

Change the color of button edit cell

You can change the color of the ButtonEdit cell while clicking, by using the CellMouseUp and MouseDown events. The following code examples are used for changing ButtonEdit color in CellMouseUp and MouseDown events.

C#

ButtonEditStyleProperties sparam; 
// Adding new ButtonEditCell model to grid.
this.gridDataBoundGrid1.Model.CellModels.Add("ButtonEdit", new ButtonEditCellModel(this.gridDataBoundGrid1.Model));
// Gets ProductName column style. 
GridStyleInfo styleparam = this.gridDataBoundGrid1.Binder.InternalColumns["ProductName"].StyleInfo;
// changing Celltype for ProductName column.
styleparam.CellType = "ButtonEdit";
//Geting ButtonEditStyleProperties for ProductName column style.
sparam = new ButtonEditStyleProperties(styleparam);
sparam.ButtonEditInfo.BackColor = Color.Orange;
sparam.ButtonEditInfo.Text = "Edit-Method";
sparam.ButtonEditInfo.TextColor = Color.White;
//Hooking CellMouseUp event.
this.gridDataBoundGrid1.CellMouseUp += new GridCellMouseEventHandler(gridDataBoundGrid1_CellMouseUp);    
//Hooking MouseDown event.
this.gridDataBoundGrid1.MouseDown += new MouseEventHandler(gridDataBoundGrid1_MouseDown);
void gridDataBoundGrid1_MouseDown(object sender, MouseEventArgs e)
{
    int rowindex, colindex = 0;
    this.gridDataBoundGrid1.ViewLayout.PointToClientRowCol(e.Location, out rowindex, out colindex,true);
    if (this.gridDataBoundGrid1[rowindex, colindex].CellType == "ButtonEdit")
    {
        // Used to change the color while clicking on it.
        sparam.ButtonEditInfo.BackColor = Color.Red;
    } 
}           
void gridDataBoundGrid1_CellMouseUp(object sender, GridCellMouseEventArgs e)
{
    if (this.gridDataBoundGrid1[e.RowIndex, e.ColIndex].CellType == "ButtonEdit")
    {
        // Used to retrieve the old color after clicking.
        sparam.ButtonEditInfo.BackColor = Color.Orange;    
    }
}

VB

Dim sparam As ButtonEditStyleProperties
' Adding new ButtonEditCell model to grid.
Me.gridDataBoundGrid1.Model.CellModels.Add("ButtonEdit", New ButtonEditCellModel(Me.gridDataBoundGrid1.Model))
' Gets ProductName column style. 
Dim styleparam As GridStyleInfo = Me.gridDataBoundGrid1.Binder.InternalColumns("ProductName").StyleInfo
' changing Celltype for ProductName column.
styleparam.CellType = "ButtonEdit"
'Geting ButtonEditStyleProperties for ProductName column style.
sparam = New ButtonEditStyleProperties(styleparam)
sparam.ButtonEditInfo.BackColor = Color.Orange
sparam.ButtonEditInfo.Text = "Edit-Method"
sparam.ButtonEditInfo.TextColor = Color.White
'Hooking CellMouseUp event.
Private Me.gridDataBoundGrid1.CellMouseUp += New GridCellMouseEventHandler(AddressOf gridDataBoundGrid1_CellMouseUp)
'Hooking MouseDown event.
Private Me.gridDataBoundGrid1.MouseDown += New MouseEventHandler(AddressOf gridDataBoundGrid1_MouseDown)
Private Sub gridDataBoundGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
 Dim rowindex As Integer, colindex As Integer = 0
 Me.gridDataBoundGrid1.ViewLayout.PointToClientRowCol(e.Location, rowindex, colindex,True)
 If Me.gridDataBoundGrid1(rowindex, colindex).CellType Is "ButtonEdit" Then
  ' Used to change the color while clicking on it.
  sparam.ButtonEditInfo.BackColor = Color.Red
 End If
End Sub
Private Sub gridDataBoundGrid1_CellMouseUp(ByVal sender As Object, ByVal e As GridCellMouseEventArgs)
 If Me.gridDataBoundGrid1(e.RowIndex, e.ColIndex).CellType Is "ButtonEdit" Then
  ' Used to retrieve the old color after clicking.
  sparam.ButtonEditInfo.BackColor = Color.Orange
 End If
End Sub

In the following screenshot, ButtonEdit color is changed while clicking.

ButtonEdit color changed during clicking

Figure 1: ButtonEdit color changed during clicking

Note:

The provided sample illustrates the functionality of GridDataBoundGrid alone. Similarly, you can apply the provided code example for GridControl to achieve the same behavior.


Conclusion

I hope you enjoyed learning about how to change the color of button in ButtonEdit cell while clicking in WinForms GridControl or GridDataBoundGrid.

You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Grid documentation 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 forumsDirect-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)
Please sign in to leave a comment
Access denied
Access denied