.NET DataGrid: Selecting the whole row when clicked on a cell

This is something I was trying for sometime and the only way I managed to do it earlier was by disabling the datagrid. The other solutions proposed were by sub-classing the datagrid control and by overriding the default behaviour. But I found a better and easy way described in this blog.

I am posting the code snippet here, just for my future reference as well as for anyone else’s benefit.

1. To handle mouse click events. When the user selects a cell by clicking on it.

private void dg_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )

2. The solution for getting the row to select when the user navigates using the arrow keys.

private void dg_CurrentCellChanged( object sender, System.EventArgs e )

I tried this on a .NETCF Pocket PC app and it worked great. Also, it solved one of the issues that I had to solve, easily.

  1. hi,

    Thank you very much for your code.
    I just need another thing to be solved.

    In .NET 2.0 datagrid we can select multiple rows from the
    mouse and when we press delete button we can delete those rows from the datagrid.

    I want to get those selected rows.
    How can i do it ?

  2. Dummy

    Ho* ho*, That’s what i need in my project. Thank to Google :))

  3. ccmos007

    works like a dream thank you :):)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: