DotNetbar 中DataGridViewX实现全选的方法


先看效果吧,第一张图片是全选的时候获取到“随机数值”列的结果


下面这张是选择了部分行获取的选中行的第二列的结果



下面来说说他的实现过程。我主要还是参照

http://www.cnblogs.com/wuhuacong/archive/2011/12/30/2307600.html

实现的。


首先你要确保你安装了DotNetBar控件,如果没有安装就要到http://www.devcomponents.com/dotnetbar/ 网站下载安装winform版本的即可。


安装完毕之后,打开vs就可以看到相应的控件



如果没有出现对应的控件,则需要手工添加控件即可。

然后就会新建windows窗体应用程序,在工具箱中找到DataGridViewX 控件,拖动到窗体上,然后添加列并设置属性:



需要特别注意的是需要填写属性“Name”、“DataPropertyName”、“ColumnType”。这三个属性分别对应着列的名称、绑定的数据列、列的类型。当然我们还需要设置datagridview的其他属性,如列标题居中,自适应窗体等属性。


接下类我们就是要添加checkbox列,其实也就是第一列,主要是改变列的显示方式。

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : DevComponents.DotNetBar.Office2007RibbonForm
    {
        CheckBox HeaderCheckBox = null;
        public Form1()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                HeaderCheckBox = new CheckBox();
                HeaderCheckBox.Size = new Size(15, 15);
                this.dataGridViewX1.Controls.Add(HeaderCheckBox);

                HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
                HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
                dataGridViewX1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
                dataGridViewX1.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindGridView()
        {
            dataGridViewX1.DataSource = GetDataSource();
        }

        private List<xaut> GetDataSource()
        {
            List<xaut> dt = new List<xaut>();
            DateTime dTime;
            for (int i = 0; i < 20; i++)
            {
                dTime = DateTime.Now;
                xaut temp = new xaut();
                temp.IsChecked = false;
                temp.RandomNo = i;
                temp.Date = dTime.ToString("yyyy-MM-dd");
                temp.Time = dTime.ToString("19:mm:ss tt");
                dt.Add(temp);
                
            }
            return dt;
        }

        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            HeaderCheckBoxClick((CheckBox)sender);
        }

        private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.CurrentCell is DataGridViewCheckBoxCell)
                dataGridViewX1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

        private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0)
                ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }

        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
            Rectangle oRectangle = this.dataGridViewX1.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);
            Point oPoint = new Point();
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
            HeaderCheckBox.Location = oPoint;
        }

        private void HeaderCheckBoxClick(CheckBox HCheckBox)
        {
            foreach (DataGridViewRow Row in dataGridViewX1.Rows)
            {
                ((DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell)Row.Cells["chkBxSelect"]).Value = HCheckBox.Checked;                
            }
            dataGridViewX1.RefreshEdit();
        }

        private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
                HeaderCheckBoxClick((CheckBox)sender);
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            //获取选中的行
            string temp = "";
            foreach (DataGridViewRow row in dataGridViewX1.Rows)
            {
                if ((bool)((DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell)row.Cells["chkBxSelect"]).Value)
                {
                    temp += row.Cells["RandomNo"].Value + "_";
                }
            }

            MessageBox.Show(temp);
        }
    }
}

在上面的红色标注部分是针对dotnetbat的,对于winform 中控件datagridview而言就不一样了。


其中表格的数据源部分是使用list<>数组。下面是绑定到表格的对象定义:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class xaut
    {
        public bool IsChecked { get; set; }
        public int RandomNo { get; set; }
        public string Date { get; set; } 
        public string Time{get;set;}
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Commons;

namespace TestGridViewCheck
{
    public partial class FrmNormalGridViewSelect : Form
    {
        CheckBox HeaderCheckBox = null;
        public FrmNormalGridViewSelect()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                HeaderCheckBox = new CheckBox();
                HeaderCheckBox.Size = new Size(15, 15);
                this.dgvSelectAll.Controls.Add(HeaderCheckBox);

                HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
                HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
                dgvSelectAll.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
                dgvSelectAll.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
            }
        }

        private void frmSelectAll_Load(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindGridView()
        {
            dgvSelectAll.DataSource = GetDataSource();
        }

        private DataTable GetDataSource()
        {
            string columnsList = "IsChecked|bool,RandomNo,Date,Time";
            DataTable dt = DataTableHelper.CreateTable(columnsList);
            DataRow dr = null;
            DateTime dTime;
            Random rnd = new Random();
            for (int i = 0; i < 100; i++)
            {
                dr = dt.NewRow();
                dTime = DateTime.Now;

                dr["IsChecked"] = "false";
                dr["RandomNo"] = rnd.NextDouble();
                dr["Date"] = dTime.ToString("yyyy-MM-dd");
                dr["Time"] = dTime.ToString("19:mm:ss tt");
                dt.Rows.Add(dr);
            }
            return dt;
        }

        private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgvSelectAll.CurrentCell is DataGridViewCheckBoxCell)
                dgvSelectAll.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }



        private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Space)
                HeaderCheckBoxClick((CheckBox)sender);
        }

        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            HeaderCheckBoxClick((CheckBox)sender);
        }

        private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0)
                    ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }

        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
            Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);
            Point oPoint = new Point();
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
            HeaderCheckBox.Location = oPoint;
        }

        private void HeaderCheckBoxClick(CheckBox HCheckBox)
        {
            foreach (DataGridViewRow Row in dgvSelectAll.Rows)
            {
                ((DataGridViewCheckBoxCell)Row.Cells["chkBxSelect"]).Value = HCheckBox.Checked;
            }
            dgvSelectAll.RefreshEdit();
        }
    }
}


在上述代码中,我们只需要把数据源部分改变成list<>即可。


源码下载   


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏雪_无痕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值