C# – How to show selected Datagrid cell value in a textBox

cvisual-studio-2008windowswindows-mobile

I am developing a Windows Mobile 6.5 application with C# using Visual Studio 2008 (I'm still a beginner), it's connected to an SQL CE database. However, I want to send the value of a selected cell in the dataGrid connected to my database to a textBox. It looks very possible, but I can't find a cell click event related to the dataGrid control, there is only one for the dataGridView that I think it's only available for the Windows Desktop applications.

Best Answer

First, wire up your click event, by selecting the GridView control and double clicking the Click field located under the Lightening Bolt:

click event

Next, expanding on an answer already posted:

private void dataGrid_Click(object sender, EventArgs e) {
  int row = dataGrid1.CurrentCell.RowNumber;
  int col = dataGrid1.CurrentCell.ColumnNumber;
  textBox1.Text = string.Format("{0}", dataGrid1[row, col]);
}

I use string.Format because sometimes your data could be null or DBNull.Value.

Related Topic