1个回答
展开全部
获取datagridview单元格修改后的值,然后用这个值去更新数据库就可以了,下面上代码
try
{
SqlConnection scon = new SqlConnection("数据库连接字");
scon.Open();
SqlCommand scmd;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string id = dataGridView1.Rows[i].Cells["id"].EditedFormattedValue.ToString();
string name = dataGridView1.Rows[i].Cells["name"].EditedFormattedValue.ToString();
string age = dataGridView1.Rows[i].Cells["age"].EditedFormattedValue.ToString();
string address = dataGridView1.Rows[i].Cells["address"].EditedFormattedValue.ToString();
string scmdStr = "update studentInfo set name='" + name + "',age='" + age + "',address='" + address + "' where id ='" + id + "'";
scmd = new SqlCommand(scmdStr, scon);
scmd.ExecuteNonQuery();
}
scon.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |