我正在使用SQL Server 2005在C#DataGridView
应用程序中使用WindowsForms
,我添加了1列,即" name"到我的DataGridView
并通过选择行到文本框显示它,但它只显示一列,即"名称"到文本框。但是我还有其他专栏,例如城市州,我还没有添加到我的DataGridView
有没有办法通过单击单列"名称"显示所有数据库记录?从DataGridView
到Textboxes
?
private void dataGridView1_CellClick
(对象发件人,DataGridViewCellEventArgs
e){
i = e.RowIndex;
DataGridViewRow row = this.dataGridView1.Rows [e.RowIndex];
textBox1.Text = row.Cells[0].Value.ToString();
String str = "server=PC2-PC;database=demo;Integrated Security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
if (e.RowIndex != -1)
{
textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[Name].Value == null ? "" : dataGridView1.Rows[e.RowIndex].Cells[Name].Value.ToString();
CallData();
}
}
答案 0 :(得分:0)
使用您的解决方案,您可以在DataGridView中构建其他列,并将Visible更改为false。通过这种方式,您可以在填写名称列时填充它们,并在用户单击行并在文本框中显示时使用他们的数据。
但这种在表单事件中使用数据库的方式不是标准代码。