C#如何把dataGridView中显示的数据读取到textBox
textBox2.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString ();
以上两种都行不通,请求解答。 展开
添加一个datagridview
添加两个textbox
添加事件dataGridView1_CellClick
public Form1()
{
樱蠢 皮兄 InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("value");
Random rd=new Random();
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = i;
dr["value"] = rd.Next();
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
dataGridView1.CellClick+=dataGridView1_CellClick;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
燃颂袭 if (e.ColumnIndex > -1 && e.RowIndex > -1)
{
//显示一个单元格。
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
//显示一行
DataGridViewRow dr = dataGridView1.Rows[e.RowIndex];
textBox2.Text = string.Empty;//清空原有值
foreach (DataGridViewCell item in dr.Cells)
{
textBox2.Text += item.Value.ToString() + " ";
}
}
}
2014-04-04