Datagrid
How can I get the value of a clicked row in a datagrid?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bart5 reputationPosted Jan 24, 2006, 6:19 AM EST
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());