海鸽 is My Lover!!
可以先显示dataGrid1中的内容,通过点击dataGrid1中选中行,就可一得到你想要的
下面的程序中,显示学生的信息,有sno,sname,sex ,在窗体上放置三个textbox
/*下面的一段程序功能是:动态获取dataGrid1中选中行的各字段的值*/
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
string myID = dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString().Trim();
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * From student where ID='" + myID + "'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
this.textBox1.Text = dr["sno"].ToString();
this.textBox2.Text = dr["sname"].ToString();
this.textBox3.Text = dr["sex"].ToString();
}
dr.Close();
cn.Close();
}
收录了.....