C#对DataGridView进行添加、修改、删除数据操作
2008-08-02 11:21
525 查看
数据库用的是本地服务器(MySql):
设定全局变量:
MySqlConnection conn;
MySqlDataAdapter adapter;
MySqlTransaction trans;
1. // 数据库联接
private System.Data.DataTable dbconn(string strSql)
{
string strconn = "host=localhost;database=test;user id=root;password=";
conn = new MySqlConnection();
conn.ConnectionString = strconn;
conn.Open();
this.adapter = new MySqlDataAdapter(strSql, conn);
System.Data.DataTable dtSelect = new System.Data.DataTable();
int rnt=this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
2. //设定DataGridView的样式
private void setDgStyle()
{
this.dgselect.Columns.Clear();
DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
colDel.DataPropertyName = "Del";
colDel.Name = "Del";
colDel.Selected = false;
colDel.FalseValue = "0";
colDel.TrueValue = "1";
colDel.Width = 40;
colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
colDel.HeaderText = "删除";
colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgselect.Columns.Insert(0, colDel);
DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "ProductsSpecID";
colID.Name = "ProductsSpecID";
colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colID.HeaderText = "产品规格ID";
colID.Width = 160;
this.dgselect.Columns.Insert(1, colID);
DataGridViewTextBoxColumn colNM = new DataGridViewTextBoxColumn();
colNM.DataPropertyName = "ProductsSpec";
colNM.Name = "ProductsSpec";
colNM.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colNM.HeaderText = "产品规格名称";
colNM.Width = 160;
this.dgselect.Columns.Insert(2, colNM);
DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
colUnit.DataPropertyName = "ProductsSpecUnit";
colUnit.Name = "ProductsSpecUnit";
colUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colUnit.HeaderText = "产品规格单位";
colUnit.Width = 180;
this.dgselect.Columns.Insert(3, colUnit);
DataGridViewTextBoxColumn colPID = new DataGridViewTextBoxColumn();
colPID.DataPropertyName = "ProductsID";
colPID.Name = "ProductsID";
colPID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colPID.HeaderText = "产品ID";
colPID.Width = 140;
this.dgselect.Columns.Insert(4, colPID);
DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
colButton.DataPropertyName = "colSearch";
colButton.Name = "colSearch";
colButton.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colButton.HeaderText = "Button";
colButton.Width = 80;
this.dgselect.Columns.Insert(5, colButton);
this.dgselect.RowHeadersWidth = 15;
this.dgselect.ColumnHeadersDefaultCellStyle.Font=new System.Drawing.Font("宋体",14);
}
3. //修改数据,并将数据提交到数据库
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;
}
设定全局变量:
MySqlConnection conn;
MySqlDataAdapter adapter;
MySqlTransaction trans;
1. // 数据库联接
private System.Data.DataTable dbconn(string strSql)
{
string strconn = "host=localhost;database=test;user id=root;password=";
conn = new MySqlConnection();
conn.ConnectionString = strconn;
conn.Open();
this.adapter = new MySqlDataAdapter(strSql, conn);
System.Data.DataTable dtSelect = new System.Data.DataTable();
int rnt=this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
2. //设定DataGridView的样式
private void setDgStyle()
{
this.dgselect.Columns.Clear();
DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
colDel.DataPropertyName = "Del";
colDel.Name = "Del";
colDel.Selected = false;
colDel.FalseValue = "0";
colDel.TrueValue = "1";
colDel.Width = 40;
colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
colDel.HeaderText = "删除";
colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgselect.Columns.Insert(0, colDel);
DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
colID.DataPropertyName = "ProductsSpecID";
colID.Name = "ProductsSpecID";
colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colID.HeaderText = "产品规格ID";
colID.Width = 160;
this.dgselect.Columns.Insert(1, colID);
DataGridViewTextBoxColumn colNM = new DataGridViewTextBoxColumn();
colNM.DataPropertyName = "ProductsSpec";
colNM.Name = "ProductsSpec";
colNM.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colNM.HeaderText = "产品规格名称";
colNM.Width = 160;
this.dgselect.Columns.Insert(2, colNM);
DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
colUnit.DataPropertyName = "ProductsSpecUnit";
colUnit.Name = "ProductsSpecUnit";
colUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colUnit.HeaderText = "产品规格单位";
colUnit.Width = 180;
this.dgselect.Columns.Insert(3, colUnit);
DataGridViewTextBoxColumn colPID = new DataGridViewTextBoxColumn();
colPID.DataPropertyName = "ProductsID";
colPID.Name = "ProductsID";
colPID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colPID.HeaderText = "产品ID";
colPID.Width = 140;
this.dgselect.Columns.Insert(4, colPID);
DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
colButton.DataPropertyName = "colSearch";
colButton.Name = "colSearch";
colButton.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
colButton.HeaderText = "Button";
colButton.Width = 80;
this.dgselect.Columns.Insert(5, colButton);
this.dgselect.RowHeadersWidth = 15;
this.dgselect.ColumnHeadersDefaultCellStyle.Font=new System.Drawing.Font("宋体",14);
}
3. //修改数据,并将数据提交到数据库
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;
}
相关文章推荐
- C#对DataGridView进行添加、修改、删除数据操作(装载)
- C#对DataGridView进行添加、修改、删除数据操作
- C#对DataGridView进行添加、修改、删除数据操作
- C#对DataGridView中的数据进行添加、修改、删除操作
- C#对DataGridView进行添加修改删除操作
- DataGridView进行添加、修改、删除数据操作-C#
- C#对DataGridView中的数据进行添加、修改、删除操作c#操作datatable
- 对DataGridView进行添加、修改、删除数据操作----C#
- C#对DataGridView进行添加、修改、删除数据操作
- C#对DataGridView中的数据进行添加、修改、删除操作
- C#中对winform的控件datagridview直接进行编辑,修改,删除操作注意的地方
- C#中对 XML节点进行添加,删除,查找和删除操作
- ASP.NET(C#)对XML文件节点的添加删除修改示例
- C# XML 添加,修改,删除Xml节点
- Active Directory如何用C#进行增加、删除、修改、查询用户与组织单位!
- 对XML文件进行的添加、删除、修改、查询操作。
- C++ builder 2007 使用ADO数据库 进行 添加 删除 修改
- 【转】c# xml添加 删除 修改数据
- C# 添加,修改,删除Xml节点
- 如何在DataGrid中进行添加、删除和修改操作