Datagrid
How can I get the value of a clicked row in a datagrid?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
BartPosted Jan 24, 2006, 11:19 AM
In .Net 1.1 this should work :
DataRowView rowView =(DataRowView)this.BindingContext[MyGrid.DataSource, MyGrid.DataMember].Current;
MessageBox.Show(rowView.Row.ItemArray[3].ToString());
In .Net 2.0 :
to get the information of the first row displayed :
DataGridViewTextBoxCell MyCell = ((DataGridViewTextBoxCell)dataGridView1.Rows[dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Displayed)].Cells[0]);
MessageBox.Show((string)MyCell.Value);
DataGridViewCheckBoxCell MyCell2 = ((DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Displayed)].Cells[1]);
MessageBox.Show(((bool)MyCell2.Value).ToString());
To get the first row selected :
DataGridViewTextBoxCell MyCell3 = ((DataGridViewTextBoxCell)dataGridView1.Rows[dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected)].Cells[0]);
MessageBox.Show((string)MyCell3.Value);
DataGridViewCheckBoxCell MyCell4 = ((DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected)].Cells[1]);
MessageBox.Show(((bool)MyCell4.Value).ToString());