C#对DataGridView中的数据进行添加、修改、删除操作

发布时间:2009年12月16日      浏览次数:572 次
private Boolean dbUpdate()
{
string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master";
System.Data.DataTable dtUpdate = new System.Data.DataTable();
dtUpdate = this.dbconn(strSql);
dtUpdate.Rows.Clear();
System.Data.DataTable dtShow = new System.Data.DataTable();
//dtShow = (DataTable)this.bindSource.DataSource;
dtShow = (System.Data.DataTable)this.dgselect.DataSource;
int p1 = dtShow.Rows.Count;
// try
// {
for (int i = 0; i < dtShow.Rows.Count; i++)
{
DataRowState rowState=new DataRowState();
rowState=dtShow.Rows[i].RowState;
if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
{
if (this.dgselect["Del", i].Value.ToString() == "1")
{
dtShow.Rows[i].Delete();
}
}
}

for (int i = 0; i < dtShow.Rows.Count; i++)
{
dtUpdate.ImportRow(dtShow.Rows[i]);
}
int num = dtUpdate.Rows.Count;
try
{
this.conn.Open();

trans = this.conn.BeginTransaction();
MySqlCommandBuilder CommandBuiler;
CommandBuiler = new MySqlCommandBuilder(this.adapter);
this.adapter.Update(dtUpdate);
trans.Commit();
this.conn.Close();
}
catch ( Exception ex)
{
MessageBox.Show(ex.Message.ToString());
trans.Rollback();
return false;
}
dtUpdate.AcceptChanges();
return true;
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!