获取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());
}