C#中DataGridView修改后直接保存到数据库实例程序,绝对可用,内附源码,有注释
主要代码:
//单元格编辑结束后触发
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
try
{
string rowid = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string cellhead = dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
string cellvalue = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (cellhead == "domain")
{ cellhead = "[domain]"; }
else { }
if (cellvalue == "True" && cellhead == "passed")
{
cellvalue = "1";
}
else
{
if (cellhead == "passed")
{
cellvalue = "0";
}
else
{ }
}
string sqlcmd = "update domain_info set " + cellhead + "='" + cellvalue + "' where id=" + rowid;
if (dbo.DBcmd(sqlcmd))
{ resultshow.Text = " 编辑成功"; }
else
{ resultshow.Text = " 编辑失败"; }
}
catch
{
MessageBox.Show("异常", "错误");
}
}
1