当我点击dataGridView
上的行时,我想在文本框中填充该行的数据?
我怎么能这样做?
此dataGridView
中的数据(例如ID=1, Name=s
...)显示在Textbox
Up ??
答案 0 :(得分:8)
您必须实施SelectionChanged
的{{1}}事件,然后检查选择的行。
DataGridView
答案 1 :(得分:3)
注册网格的MouseClick事件并使用以下代码。
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
textBox1.Text = dr.Cells[0].Value.ToString();
// or simply use column name instead of index
//dr.Cells["id"].Value.ToString();
textBox2.Text = dr.Cells[1].Value.ToString();
textBox3.Text = dr.Cells[2].Value.ToString();
textBox4.Text = dr.Cells[3].Value.ToString();
}
在加载事件中添加以下行
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
答案 2 :(得分:0)
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//it checks if the row index of the cell is greater than or equal to zero
if (e.RowIndex >= 0)
{
//gets a collection that contains all the rows
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
//populate the textbox from specific value of the coordinates of column and row.
txtid.Text = row.Cells[0].Value.ToString();
txtname.Text = row.Cells[1].Value.ToString();
txtsurname.Text = row.Cells[2].Value.ToString();
txtcity.Text = row.Cells[3].Value.ToString();
txtmobile.Text = row.Cells[4].Value.ToString();
}
}