Posts

Showing posts from October, 2020

Button Column Click Event of DataGridView

Image
Suppose, we have two button column in a dataGridView. One is "Edit" and another is "Delete". When "Edit" column's button is pressed, some tasks need to perform. Same goes for "Delete" column's buttons. To do so, select CellContentClick Event of dataGridView. Then do the following code:   private void studentDetailsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { var senderGrid = (DataGridView)sender; if(e.RowIndex >= 0) // To avoid header of the columns... { if (senderGrid.Columns[e.ColumnIndex] == senderGrid.Columns["nameOfButtonColumn1"]) { } else if (senderGrid.Columns[e.ColumnIndex] == senderGrid.Columns["nameOfButtonColumn2"]) { } } } If you don't know the name of your button column, see  this

Name of the button column in dataGridView

Image
Here I have two button column named Edit and Delete. To get the column's name, do the following steps.  Step 1:  Select the dataGridView and click on the arrow sign. Step 2:  Select the Edit Columns option. Step 3:  On the left side, a list of column's header will be shown of the dataGridView. To change the properties of Edit Column select Edit . On the right side, it's properties will be shown. The Name under Design section indicates the name of the Edit column. 

Add Button Column in DataGridView

Image
Do the following steps . Step 1: Click on the dataGridView. You will see an arrow sign on the upper right corner. Click on it. Step 2: Select Add Column option. Step 3: Set the Name. Here Name is the button's property name. It is not the display text of button. Set the Type as DataGridViewButtonColumn . Set the Header text. It shows the column's head name. Then press Add and close it. Step 4:  Now a button column has been added to the dataGridView. But the text of the button has not set yet. So click on the arrow sign again and select Edit Columns. Step 5: On the left side, select the column header you want to edit. We select Edit column to change it's properties. Then set the Text and make UseColumnTextForButton true . Then press OK. Step 6: Build and Start your Project. In my database, student table has 3 rows, that's why 3 Update button has been added to dataGridView's Edit column. To do the button column click event, see  this