使用Winform进行数据库的客户端程序设计或者其他与数据库相关的操作时,几乎都会用到Datagridview控件,它能够方便的将数据表按照报表的格式显示,并且支持多种操作。

但是DataGridview控件显示的数据并不是直接来自数据库:往往是先通过new Dataset()创建一个DataSet,然后通过SqlDataAdapter将数据库中的数据与DataSet关联。然后,将DataSet中的数据填入DataGridview。

            string str = "Data Source=主机名或者IP地址; Initial Catalog=数据库名; User Id=sa; Password=******;";
            this.connection = new SqlConnection(str);
            cmd = "select * from " + 要查询的表名;

            this.ds = new DataSet();
            this.da = new SqlDataAdapter(cmd, connection);
            this.da.Fill(this.ds, table);

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = 要查询的表名;

            上面这段代码将数据库中的“要查询的表名”的内容显示到datagridview中。

这在显示数据表的内容时不会出现任何问题,但如果想通过直接删除或修改DataGridview数据来修改或删除数据表中对应的数据则需要添加额外的操作。

           

上图中ID、name、college是从数据库中直接读取的,后面两列的内容则是我通过代码添加上去的,通过点击其中的内容来修改或删除对应行的内容。

            this.dTable = this.ds.Tables[table];

     System.Windows.Forms.DataGridViewCellStyle ModifyCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
            ModifyCellStyle.NullValue = "修改并保存";

            System.Windows.Forms.DataGridViewCellStyle DeleteCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
            DeleteCellStyle.NullValue = "删除该行";

            //确定“要查询的表”的列数

            int columeCount = ds.Tables[0].Columns.Count;

            //将列名写到dataGridView1中

            for (int i = 0; i < columeCount; i++)
            {
                DataGridViewTextBoxColumn colume = new DataGridViewTextBoxColumn();
                colume.HeaderText = ds.Tables[0].Columns[i].ColumnName;
                dataGridView1.Columns.Add(colume);
            }

            //额外添加的两列为DataGridViewLinkColumn类型 

            DataGridViewLinkColumn cModify = new DataGridViewLinkColumn();
            cModify.HeaderText = "修改数据";

            //并且将他们默认显示内容设置如下
            cModify.DefaultCellStyle = ModifyCellStyle;

            //添加列
            dataGridView1.Columns.Add(cModify);

            DataGridViewLinkColumn cDelete = new DataGridViewLinkColumn();
            cDelete.HeaderText = "删除数据";
            cDelete.DefaultCellStyle = DeleteCellStyle;
            dataGridView1.Columns.Add(cDelete);

            //确定数据库中表的行数

            int rowCount = ds.Tables[0].Rows.Count;

            for (int j = 0; j < rowCount; j++)
            {
                dataGridView1.Rows.Add(1);
                for (int k = 0; k < columeCount; k++)
                {
                    dataGridView1.Rows[j].Cells[k].Value = ds.Tables[0].Rows[j][k];
                }
            }

上面的代码将生成如下图所示的表格:

          

先在要做的是处理点击“删除改行的消息”,这首先需要让Datagridview响应该点击消息

          

然后在对应的方法中添加如下代码

               //用来判断当前所点击的行

       int rowIndex = dataGridView1.CurrentRow.Index;
                dCurrent = dTable.Rows[rowIndex];
                dCurrent.Delete();

                this.dTable = this.ds.Tables[table];

                //通过下面两天语句,使DataSet与数据库同步

                SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da);
                da.Update(ds, table);
                //删除之前dataGridView1绘制的表格
                dataGridView1.Rows.Clear();

                int rowCount = ds.Tables[0].Rows.Count;
                int columeCount = ds.Tables[0].Columns.Count;

                //使用更新后的数据绘制表格

                for (int j = 0; j < rowCount; j++)
                {
                    dataGridView1.Rows.Add(1);
                    for (int k = 0; k < columeCount; k++)
                    {
                        dataGridView1.Rows[j].Cells[k].Value = ds.Tables[0].Rows[j][k];
                    }
                }

                //OK