我尝试使用 datagridview 更新我的数据库,没有错误,但是当我单击“保存”按钮时没有进行任何更改。你能帮我吗..这是我的代码提前谢谢。.
private void EditRecord_Load(object sender, EventArgs e) {
LoadData();
}
void LoadData() {
string query = "SELECT *FROM Record";
SqlDataAdapter da = new SqlDataAdapter(query, con);
SqlCommandBuilder sbuilder = new SqlCommandBuilder(da);
DataTable dtable = new DataTable();
da.Fill(dtable);
BindingSource bsource = new BindingSource();
bsource.DataSource = dtable;
dgv.DataSource = bsource;
}
private void btnSave_Click(object sender, EventArgs e) {
if (dgv.RowCount > 1) {
for (int x = 0; x < dgv.RowCount - 1; x++) {
if (dgv.Rows[x].Cells[0].Value.ToString() == "") {
SqlCommand cmdSave = new SqlCommand("UPDATE tblRecord SET FName=@FName, Address=@Address, ContactNo=@ContactNo WHERE IdNo=@IdNo", con);
{
cmdSave.Parameters.Add("@IdNo", SqlDbType.VarChar).Value = dgv.Rows[x].Cells[0].Value;
cmdSave.Parameters.Add("@FName", SqlDbType.VarChar).Value = dgv.Rows[x].Cells[1].Value;
cmdSave.Parameters.Add("@Address", SqlDbType.VarChar).Value = dgv.Rows[x].Cells[2].Value;
cmdSave.Parameters.Add("@ContactNo", SqlDbType.VarChar).Value = dgv.Rows[x].Cells[3].Value;
}
con.Open();
cmdSave.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Updated!");
}
}
}
LoadData();
}