我的故事

海韵
网站日历
73 2018 - 10 48
 123456
78910111213
14151617181920
21222324252627
28293031

栏目导航
    全部文章
    随笔杂谈
    文学欣赏
    休闲娱乐
    热点综述
    信息技术
最新评论
统计信息
日记搜索

本站搜索百度搜索
其他信息
BLOG编码
RSS 1.0
RSS 2.0
未知 [信息技术] C#对DataGridView进行添加修改删除操作    阅读:12615    

C#对DataGridView进行添加修改删除操作
作者:不详 编辑:海韵 发布日期:2007-03-01 点击量:12615 来源:haiyun.net

数据库用的是本地服务器(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;

        }

////////////////////////////////////////////////

绑定后焦点未离开时不会保存的解决办法:
this.BindingContext[this.dataGridView1.DataSource].EndCurrentEdit();

////////////////////////////////////////////////

 

//////////////////////////////////////////////

先构建内存表,再保存

private DataTable DataGridToDataTable(DataGridView dgList)
{
DataTable newdt = new DataTable();
try
{
DataTable dt2 = (DataTable)dgList.DataSource;
int colCount = dgList.Columns.GetColumnCount(DataGridViewElementStates.Visible);
string[] ColumnNames = new string[colCount];
int j = 0;
for (int i = 0; i < dgList.Columns.Count; i++)
{
if (dgList.Columns[i].Visible)
{
ColumnNames[j] = dgList.Columns[i].Name.ToString();
j++;
}
}

foreach (string ColumnName in ColumnNames)
{
newdt.Columns.Add(ColumnName);
}

for (int i = 0; i < dt2.Rows.Count; i++)
{
DataRow drNew = newdt.NewRow();
foreach (string ColumnName in ColumnNames)
{
drNew[ColumnName] = dt2.Rows[i][ColumnName];
}
newdt.Rows.Add(drNew);
newdt.AcceptChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return newdt;

}

DataTable dt = this.DataGridToDataTable(dgList);
SqlDataAdapter + SqlCommandBuilder ..

调用SqlDataAdapter.Update(DataTable);


[ 于 2007年3月1日 0:00:00 周四  ]  

网友评论
正在加载评论...


免责申明 | 版权申明 | 联系我们 | 关于
Copyright © 2001-2014 Haiyun.Net All Rights Reserved
地址:深圳市南山区南油天安工业区1栋3楼 邮编:518000
有我可以帮您的吗?  
粤ICP备05017321号