Sunday, September 6, 2009

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.

3 comments:

liferockzzzz said...

If I use Datalist in place of datagrid view in web application
so "cell content click " event is also present in data list or I have to apply some other procedure Guide me

Adil Mughal said...

@liferockzzzz: Can you please provide some details what you exactly want?

liferockzzzz said...

actually my project is of "online shopping cart"for displaying products I use Datalist and for showing details popup window will open the problem is I Didn't know how to connects details with each product so on clicking on products its detail will show in pop up window?

Post a Comment