Reputation:
How can get the value of selected row in grid view without events function ? I using something like that but it doesn't work:
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
Upvotes: 0
Views: 5734
Reputation: 63327
Your problem is that you don't actually know how to select a row, you have to select a row by clicking on the row header, or if you want it simpler, just use this code:
gvMember.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
then you can just click on the row itself. That way, it will ensure that there is always at least 1 row selected. If you don't want that full row select mode, you have to check the SelectedRows.Count
and notify user to select row by clicking on the row header like this:
if(gvMember.SelectedRows.Count > 0){
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
//... other code
} else {
MessageBox.Show("There is not any row selected, you select row by clicking on the row header!");
}
If you want to get the id corresponding to the current row, you can use the CurrentRow
property:
int select= Convert.ToInt32(gvMember.CurrentRow.Cells[0].Value.ToString());
Upvotes: 1