)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 20, 2015 |
Last Revised Date: February 11, 2020
Tags: styling

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.

Samples:

C#: ButtonEditCell_C#

VB: ButtonEditCell_VB

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
Live Chat Icon