C#中DataGridView修改后直接保存到数据库实例程序,绝对可用,内附源码,有注释主要代码://单元格编辑结束后触发privatevoiddataGridView1_CellEndEdit_1(objectsender,DataGridViewCellEventArgse){try{stringrowid=this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();stringcellhead=dataGridView1.Columns[e.ColumnIndex].DataPropertyName;stringcellvalue=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{}}stringsqlcmd="updatedomain_infoset"+cellhead+"='"+cellvalue+"'whereid="+rowid;if(dbo.DBcmd(sqlcmd)){resultshow.Text="编辑成功";}else{resultshow.Text="编辑失败";}}catch{MessageBox.Show("异常","错误");}}
1