0
我已经创建了一个表单,其中有4个标签的教师ID,教师姓名,部门和说明以及相应的文本框。我通过ADD按钮(使用插入查询)将值插入到数据库中。我使用datagrid视图来显示数据。如何使用数据网格视图获取文本框中显示的特定数据
现在的问题是,我想当iIselect datagridview的一行,那么它应该显示u =它的数据在各自的文本框中。 我试过下面的代码,但没能正确写入。
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
con.Open();
SqlCommand com = new SqlCommand("select * from teacher2", con);
SqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dr.Close();
dataGridView1.DataSource = dt;
if (dr.HasRows)
dr.Read();
txtteacherid.Text = dr[0].ToString();
txtteachername.Text = dr[1].ToString();
txtdepartment.Text = dr[2].ToString();
txtdescription.Text = dr[3].ToString();
}