datagridview的行全选

本文介绍了如何在WinForm的DataGridView中实现行全选功能,包括在列头添加CheckBox,处理Checkbox的CheckedChanged事件以同步所有行的选中状态,并解决水平滚动时自定义Checkbox显示问题的代码实现。

第一步:

 private void BoxDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

        {
            if (e.RowIndex == -1 & e.ColumnIndex == 0)
            {
                Point p = GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                p.Offset(2, 0);
                SelectAll.Location = p;
                SelectAll.Size = GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Size;
                SelectAll.Visible = true;
                SelectAll.BringToFront();
            }

        }


在实现cellPainting在里面加入一个checkbox,这个是在列头加入


第二步:

     private void SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            EndEdit(DataGridViewDataErrorContexts.Commit);
            
            for (int i = 0; i < RowCount; i++)
            {
                Rows.SharedRow(i).SetValues(SelectAll.Checked);
            }
            CommitEdit(DataGridViewDataErrorContexts.Commit);
        }


实现在列头添加的checkbox的checkedchanged方法,在里面必须要注意for外边的两行代码,如果没有这两行代码会导致第一行是无法选中的,除非你先点击datagridview中的某行。

(注意)winform的datagridview列头是不带checkbox的,如果要就需要自己画上去。

第三步:

如果整个datagridview出现了水平的滚动条那么在滚动到comboboxcheckcolumn不显示的时候,刚刚画的combobox还是会显示出来的,所以在scroll事件里面做一下处理


 private void BoxDataGridView_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                scrolling = true;
        }


完整代码如下:

 private CheckBox SelectAll = new CheckBox();
        private bool scrolling = false;


        public BoxDataGridView() : this(new List<CPackageInfo>())
        {
            SelectAll.BackColor = Color.Transparent;
            SelectAll.Visible = false;
            this.CellPainting += new DataGridViewCellPaintingEventHandler(BoxDataGridView_CellPainting);
            this.Scroll += new ScrollEventHandler(BoxDataGridView_Scroll);
            SelectAll.CheckedChanged += new EventHandler(SelectAll_CheckedChanged);
            Controls.Add(SelectAll);
        }


        private void BoxDataGridView_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
                scrolling = true;
        }


        private void SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            EndEdit(DataGridViewDataErrorContexts.Commit);
            
            for (int i = 0; i < RowCount; i++)
            {
                Rows.SharedRow(i).SetValues(SelectAll.Checked);
            }
            CommitEdit(DataGridViewDataErrorContexts.Commit);
        }


        private void BoxDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 & e.ColumnIndex == 0)
            {
                scrolling = false;
                Point p = GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                p.Offset(2, 0);
                SelectAll.Location = p;
                SelectAll.Size = GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Size;
                SelectAll.Visible = true;
                SelectAll.BringToFront();
            }
            else
            {
                if (scrolling)
                    SelectAll.Visible = false;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值