Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from selected datagridview row and which event?

I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_click maybe), the contents of that row must be displayed in the text boxes,

i tried out this codes

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {     MessageBox.Show("CEll Double_Click event calls");     int rowIndex = e.RowIndex;     DataGridViewRow row = dataGridView1.Rows[rowIndex];     textBox5.Text = row.Cells[1].Value; }  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {     int rowIndex = e.RowIndex;     DataGridViewRow row = dataGridView1.Rows[rowIndex];     textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value; } 

there are many other textboxes, but the main problem is that none of the event seems to be triggered, what event should i use to do so, or is there some property of datagrid that i might have set wrong? Any help would be appreciated...:(

like image 660
Samy S.Rathore Avatar asked Jun 29 '12 11:06

Samy S.Rathore


People also ask

How to get the Selected row in DataGridView?

To get the selected rows in a DataGridView controlUse the SelectedRows property. To enable users to select rows, you must set the SelectionMode property to FullRowSelect or RowHeaderSelect.

How to get the Selected row in GridView c#?

When a row is selected in a GridView control, use the SelectedRow property to retrieve the GridViewRow object that represents that row. This is the same as retrieving the GridViewRow object at the index specified by the SelectedIndex property from the Rows collection.


1 Answers

You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:

private void dataGridView_SelectionChanged(object sender, EventArgs e)  {     foreach (DataGridViewRow row in dataGridView.SelectedRows)      {         string value1 = row.Cells[0].Value.ToString();         string value2 = row.Cells[1].Value.ToString();         //...     } } 

You can also walk through the column collection instead of typing indexes...

like image 184
Vale Avatar answered Sep 28 '22 23:09

Vale