Real World Scenario: DataGridView Link Click

Finally, I got time to blog another post on Real World Scenario. This time I received an email from a final year student. As mentioned earlier, the purpose of blogging about these issues is to share solution with others facing similar problem.

Scenario: (copied from email)

I’m facing some problems in my project. In one of the form I am using DataGridview to display my data… ! as mention in the attached JPEG. All columns are Databound. (except View Detail Column).
I want, when i click on ‘View’ all the data of particular row will display it new form im detail… ! Is it possible ??

The answer to this query is quite simple. DataGridView exposes an event ‘CellContentClick’ to which you can subscribe and handle the event. Consider the code snippet:

//subscribe the event first
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);

//handle the event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 4) //4 is the index of Column with link (DataGridViewLinkColumn)
    {
        DetailForm detailForm = new DetailForm();
        detailForm.ShowDialog();
    }
}

and that will give you the desired functionality:) Thank you again for contacting me. If you have any issue related to .NET, send me an email and I will try my level best to solve the query for you.