Groups | Blog | Home
all groups > dotnet windows forms > february 2008 >

dotnet windows forms : Selecting a row and the values in the cells


thewanz
2/7/2008 2:12:04 PM
Hello,
I would like to take the values contained in columns of a selected row and
display them in textboxes for editing. When I dbl click the datagridview
control it creates an event handler for the selected cell. Isn't there a
specific event handler for a selected row?

Norman Yuan
2/7/2008 4:42:17 PM
Firstly, you may have already know you can make cell, row or column of
DataGridView select-able. In your case, make sure the DataGridView's
SelectionMode is set to RowHeaderSelect, or FullRowSelect.

Then, you can handle SelectionChanged event:

private void DataGridView1_SelectionChanged(...)
{
if (DataGridView1.SelectedRows.Count==0) return;

//Do something with the SelectedRows, it could be one row, or multiple
rows. For example:

Label1.Text=DataGridView1.SelectedRows[0].Cells[0].Value.ToString();
...
}

[quoted text, click to view]
thewanz
2/8/2008 9:16:01 AM
Excellent! This works, thanks for the tip on the selection mode, too!
Cheers,
Wnz

[quoted text, click to view]
AddThis Social Bookmark Button