Responding to DataGrid selection

G

Guest

With a .Net 2's WinForms which contain
1) a DataGrid which is assigned to a DataSet's DataTable (some columns are
"hidden" on screen)
2) a few TextBox which are aligned to different fields within the same
DataTable

What event do I code for the .Net DataGrid such that the TextBoxes's value
are updated as different rows are selected?
 
M

Morten Wennevik [C# MVP]

With a .Net 2's WinForms which contain
1) a DataGrid which is assigned to a DataSet's DataTable (some columnsare
"hidden" on screen)
2) a few TextBox which are aligned to different fields within the same
DataTable

What event do I code for the .Net DataGrid such that the TextBoxes's value
are updated as different rows are selected?

Hi Patrick,

Use DataBinding for this. Bind the Text property of the TextBox to the Column you want to display. The code below demonstrates how to do this.

DataGridView dataGridView1 = new DataGridView();
TextBox textBox1 = new TextBox();

protected override void OnLoad(EventArgs e)
{
this.Controls.Add(dataGridView1);
this.Controls.Add(textBox1);
textBox1.Location = new Point(dataGridView1.Width + 5, 0);

DataSet set = new DataSet();
set.Tables.Add(new DataTable());
set.Tables[0].Columns.Add("ID", typeof(System.Int32));
set.Tables[0].Columns.Add("Value", typeof(System.String));
DataRow row = set.Tables[0].NewRow();
row["ID"] = 1;
row["Value"] = "One";
set.Tables[0].Rows.Add(row);
row = set.Tables[0].NewRow();
row["ID"] = 2;
row["Value"] = "Two";
set.Tables[0].Rows.Add(row);
row = set.Tables[0].NewRow();
row["ID"] = 3;
row["Value"] = "Three";
set.Tables[0].Rows.Add(row);

dataGridView1.DataSource = set.Tables[0];
dataGridView1.Columns[0].Visible = false;

textBox1.DataBindings.Add("Text", set.Tables[0], "ID");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top