WinForm中利用DataGridView控件对数据进行批量操作

 
在Winform中将数据库中的多张表同时显示在多个DataGridView中的方法
 
SQL小知识点总结(一)

WinForm中利用DataGridView控件对数据进行批量操作  

2009-08-28 23:31:15|  分类: C# |  标签: |举报 |字号 订阅

  有时在项目中,我们需要对数据进行批量的操作,比如:批量删除、批量添加数据等,以实现更高效率的执行  在Winform中,我们利用DataGridView控件来对数据进行批量操作。 效果如下图:WinForm中利用DataGridView控件对数据进行批量操作 - Bill  - 已经开始···

示例代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BatExecute
{
    public partial class Form1 : Form
    {
        //声明一个全局的DataSet
        DataSet ds = new DataSet();
        //声明一个全局的SqlDataAdapter
        SqlDataAdapter da = new SqlDataAdapter();
        //声明一个全局的SQLParameter
        SqlParameter param = new SqlParameter();

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体加载时,在DataGridView中显示数据
            using (da = new SqlDataAdapter("select * from product", DBService.Conn))
            {
                da.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0];
            }
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //批量添加多条数据
            string addSQL = "insert into product (productId,productName,productPrice,productDescription)values (@id,@name,@price,@description)";
            //在数据源添加新数据
            da.InsertCommand = new SqlCommand(addSQL,DBService.Conn);
            param = da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar, 20);
            //建立参数与数据库字段的关系
            param.SourceColumn = "productName";
            //获取对应列的值
            param.SourceVersion = DataRowVersion.Current;

            //下面是用来添加其余的列
            param = da.InsertCommand.Parameters.Add("@price",SqlDbType.NVarChar,10);
            param.SourceColumn = "productPrice";
            param.SourceVersion = DataRowVersion.Current;

            param = da.InsertCommand.Parameters.Add("@description",SqlDbType.NVarChar,100);
            param.SourceColumn = "productDescription";
            param.SourceVersion = DataRowVersion.Current;

            param = da.InsertCommand.Parameters.Add("@id",SqlDbType.Int);
            param.SourceColumn = "productId";
            param.SourceVersion = DataRowVersion.Current;

            if (ds.HasChanges())  //如果DataSet对象(即DataGridView,DataGridView与DataSet的关系如同人和影子的关系)改变,则返回真
            {
                MessageBox.Show("批量添加成功!");
                da.Update(ds.Tables[0]);
            }
        }

        /// <summary>
        /// 批量修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModify_Click(object sender, EventArgs e)
        {
            //批量修改数据
            string modifySQL = "update product set productName=@name,productprice=@price,productDescription=@description where productid=@id";
            //用于更新数据源中的数据
            da.UpdateCommand = new SqlCommand(modifySQL,DBService.Conn);
            //建立参数与数据库字段的关系
            param = da.UpdateCommand.Parameters.Add("@name",SqlDbType.NVarChar,20);
            param.SourceColumn = "productName";
            param.SourceVersion = DataRowVersion.Current;

            param = da.UpdateCommand.Parameters.Add("@price",SqlDbType.NVarChar,10);
            param.SourceColumn = "productprice";
            param.SourceVersion = DataRowVersion.Current;

            param = da.UpdateCommand.Parameters.Add("@description",SqlDbType.NVarChar,100);
            param.SourceColumn = "productDescription";
            param.SourceVersion = DataRowVersion.Current;

            param = da.UpdateCommand.Parameters.Add("@id",SqlDbType.NVarChar,10);
            param.SourceColumn = "productid";
            param.SourceVersion = DataRowVersion.Original;

            if (DialogResult.OK == MessageBox.Show("确认修改吗?", "警告!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
            {
                if (ds.HasChanges())
                {
                    da.Update(ds.Tables[0]);
                    MessageBox.Show("批量修改成功!");
                }
            }
        }

        /// <summary>
        /// 批量删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            string deleteSQL = "delete from product where productid=@id";
            da.DeleteCommand = new SqlCommand(deleteSQL,DBService.Conn);

            param = da.DeleteCommand.Parameters.Add("@id",SqlDbType.Int);
            param.SourceColumn = "productid";
            param.SourceVersion = DataRowVersion.Original;

            ds.Tables[0].Rows[this.dataGridView1.SelectedRows[0].Index].Delete();   //注意这里,如果没写,不能执行删除
            da.Update(ds.Tables[0]);
            MessageBox.Show("批量删除成功!");

        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值