How to Access Record by DataGridViewButton: Windows Forms

November 01, 2013 , , 2 Comments

I have created a DataGridViewButton for each particular record of DataGridView, as in my earlier post. Adding a button is not sufficient in programming, it should perform some functions when clicked, so we have to generate its click event.

In simple words, we can’t generate a click event of this DataGridViewButton using double click or from the events section. As this is in datagridview so generate Cell_Click event of this datagridview. Now, I have added this button in first column, so I can check it on the same.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
MessageBox.Show("Column: " + e.ColumnIndex + " and Row: " + e.RowIndex);

By this simple code, the debugger will execute all your line of code, written in the space provided. ColumnIndex contains the column index and RowIndex contains row index, when this event triggered.

Let suppose, we want to access the record of the clicked row of datagridview. I have bound that datagridview with list of student class, so the following C# lines of code will get the instance of student, which is currently focused by mouse click event.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
Student stu = dataGridView1.Rows[e.RowIndex].DataBoundItem as Student;
MessageBox.Show("Name: " + stu.Name + "\r" + "Age: " + stu.Age
+ "\r" + "City" + stu.City + "\r"
+ "Dob: " + stu.Dob);

The first line will get the particular record, which is of student type. When we run the project and click on the datagridviewbutton it will show a message box, containing the detail of particular student.

How to access record by Datagridviewbutton in C#: Windows Forms

Subhash Sharma

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google


  1. The information on this web log is extremely helpful and extremely attention-grabbing.