C# winfrom 中datagridview中checkbox的使用方法

眾所周知,datagridview控件是CS架構(gòu)中用的比較頻繁的一個控件,里面提供了checkbox列的功能,可是卻沒有在列頭給出checkbox控件用于全選/全部取消所有行的功能,確實是個遺憾,這里就通過繪制實現(xiàn)這個功能.

效果圖:

一.創(chuàng)建一個表,里面包含bit字段,datagridview的DataGridViewCheckBoxColumn列會自動將其轉(zhuǎn)換成checkbox列

二. 界面上調(diào)用

DataGridView綁定完數(shù)據(jù)以后,在第一列繪制CheckBox列頭,可以在DataBindingComplete事件中處理

grid.DataBindingAfter += new Action(DataBindingAterEventHandler);

處理列頭全選事件

行多選處理

實現(xiàn)核心代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Windows.Forms;

using System.Windows.Forms.VisualStyles;

namespace YXSY.UI.UICommon

{

///

/// 功能描述:在DataGridView列頭繪制CheckBox控件

///

///

/// 創(chuàng)建日期:2010-12-03, 作者:Huang.Ting

///

//實現(xiàn)全選的事件參數(shù)類

public class DataGridviewCheckboxHeaderEventHander : EventArgs

{

private bool checkedState = false;

public bool CheckedState

{

get { return checkedState; }

set { checkedState = value; }

}

}

//與事件關(guān)聯(lián)的委托

public delegate void DataGridviewCheckboxHeaderCellEventHander(object sender, DataGridviewCheckboxHeaderEventHander e);

//重寫 DataGridViewColumnHeaderCell

public class DataGridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell

{

private Point checkBoxLocation;

private Size checkBoxSize;

private bool isChecked = false;

private Point cellLocation = new Point();

private CheckBoxState cbState = CheckBoxState.UncheckedNormal;

public event DataGridviewCheckboxHeaderCellEventHander OnCheckBoxClicked;

//繪制列頭checkbox

protected override void Paint(Graphics g,

Rectangle clipBounds,

Rectangle cellBounds,

int rowIndex,

DataGridViewElementStates dataGridViewElementState,

object value,

object formattedValue,

string errorText,

DataGridViewCellStyle cellStyle,

DataGridViewAdvancedBorderStyle advancedBorderStyle,

DataGridViewPaintParts paintParts)

{

base.Paint(g, clipBounds, cellBounds, rowIndex,dataGridViewElementState, value,formattedValue, errorText, cellStyle,advancedBorderStyle, paintParts);

Point p = new Point();

Size s = CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal);

//列頭checkbox的X坐標

p.X = cellBounds.Location.X +(cellBounds.Width / 2) - (s.Width / 2) - 1;

//列頭checkbox的Y坐標

p.Y = cellBounds.Location.Y +(cellBounds.Height / 2) - (s.Height / 2);

cellLocation = cellBounds.Location;

checkBoxLocation = p;

checkBoxSize = s;

if (isChecked)

cbState = CheckBoxState.CheckedNormal;

else

cbState =? CheckBoxState.UncheckedNormal;

//繪制復選框

CheckBoxRenderer.DrawCheckBox(g, checkBoxLocation, cbState);

}

///

/// 響應(yīng)點擊列頭checkbox單擊事件

///

protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)

{

Point p = new Point(e.X + cellLocation.X, e.Y + cellLocation.Y);

if (p.X >= checkBoxLocation.X && p.X <= checkBoxLocation.X + checkBoxSize.Width && p.Y >= checkBoxLocation.Y && p.Y <= checkBoxLocation.Y + checkBoxSize.Height)

{

isChecked = !isChecked;

//獲取列頭checkbox的選擇狀態(tài)

DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander();

ex.CheckedState = isChecked;

//此處不代表選擇的列頭checkbox,只是作為參數(shù)傳遞。應(yīng)該列頭checkbox是繪制出來的,無法獲得它的實例

object sender = new object();

if (OnCheckBoxClicked != null)

{

//觸發(fā)單擊事件

OnCheckBoxClicked(sender, ex);

this.DataGridView.InvalidateCell(this);

}

}

base.OnMouseClick(e);

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • //出自51博客:www.Amanda0928.51.com 第一章 一、選擇題 1.B; (typedef ,t...
    Damongggggg閱讀 11,426評論 0 1
  • /*類和對象 1.類和實例化定義類聲明類定義成員函數(shù)數(shù)據(jù)成員的賦值使用類的對象。 2.構(gòu)造函數(shù)數(shù)據(jù)封裝默認構(gòu)造函數(shù)...
    aofeilin閱讀 1,197評論 1 2
  • 基本概念 1a general-purpose programming language用于創(chuàng)建計算機程序。藝術(shù)類...
    伍帆閱讀 1,516評論 0 1
  • 人生在世,都喜歡對比,喜歡互相傷害 你羨慕朋友忙碌,充實,高薪,有追求 朋友羨慕你悠閑,自由,普通,懂享受 小孩羨...
    lumyblue閱讀 1,555評論 0 0

友情鏈接更多精彩內(nèi)容