.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 )
{
dg.Select(dg.CurrentRowIndex);
}
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 )
{
dg.Select(dg.CurrentRowIndex);
}
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.
January 16, 2008 at 9:24 am
hi,
Thank you very much for your code.
I just need another thing to be solved.
Ex:
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 ?
December 4, 2008 at 3:52 pm
Ho* ho*, That’s what i need in my project. Thank to Google
)
February 8, 2012 at 12:54 pm
works like a dream thank you
:)