在開發(fā)一個(gè)個(gè)人項(xiàng)目的時(shí)候,有客戶反映默認(rèn)GridView多選操作不是很方便和理想,想在列表的左邊增加一列可以勾選,并且最好支持列表頭部全選的操作,否則數(shù)據(jù)多的時(shí)候一個(gè)個(gè)勾選要到天荒地老。
基于以上需求,找了不少例子進(jìn)行比較,并對(duì)代碼進(jìn)行測(cè)試改進(jìn),終于完成了以上的功能了, 并且由于我本身做了多套界面的處理,因此,基于傳統(tǒng)的DataGridView全選操作不能少,而且基于DevExpress控件的GridView全選操作也應(yīng)該支持,呵呵。
無圖無真相,下面先上圖介紹兩種不同的效果,然后在詳細(xì)介紹代碼的實(shí)現(xiàn)。
1)DevExpress控件的GridView的實(shí)現(xiàn)多選操作
先講DevExpress控件的GridView的實(shí)現(xiàn),要實(shí)現(xiàn)的功能基本上是處理單擊全選操作、重新繪制表頭等操作,首先在加載第一步實(shí)現(xiàn)相關(guān)的事件和操作,如下所示。
this.gridView1.Click += new System.EventHandler(this.gridView1_Click);
this.gridView1.CustomDrawColumnHeader +=
new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler
(this.gridView1_CustomDrawColumnHeader);
this.gridView1.DataSourceChanged +=
new EventHandler(gridView1_DataSourceChanged);
然后就是實(shí)現(xiàn)里面的事件操作了,對(duì)應(yīng)的代碼如下所示。
private void gridView1_Click(object sender, EventArgs e)
{
if (DevControlHelper.ClickGridCheckBox(this.gridView1, "Check", m_checkStatus))
{
m_checkStatus = !m_checkStatus;
}
}
private void gridView1_CustomDrawColumnHeader
(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column != null && e.Column.FieldName == "Check")
{
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DevControlHelper.DrawCheckBox(e, m_checkStatus);
e.Handled = true;
}
}
void gridView1_DataSourceChanged(object sender, EventArgs e)
{
GridColumn column =
this.gridView1.Columns.ColumnByFieldName( "Check");
if (column != null)
{
column.Width = 80;
column.OptionsColumn.ShowCaption = false;
column.ColumnEdit = new RepositoryItemCheckEdit();
}
}
其中單擊和繪制表頭的操作,交給另外一個(gè)類DevControlHelper來獨(dú)立進(jìn)行處理,數(shù)據(jù)源變化gridView1_DataSourceChanged實(shí)現(xiàn)的操作是尋找對(duì)應(yīng)的全選列,并設(shè)置列寬、隱藏表頭標(biāo)題,并設(shè)置為復(fù)選框樣式。
DevControlHelper 類的實(shí)現(xiàn)代碼如下所示:
public static void DrawCheckBox
(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
{
RepositoryItemCheckEdit repositoryCheck =
e.Column.ColumnEdit as RepositoryItemCheckEdit;
if (repositoryCheck != null)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
info = repositoryCheck.CreateViewInfo() as
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
painter = repositoryCheck.CreatePainter()
as DevExpress.XtraEditors.Drawing.CheckEditPainter;
info.EditValue = chk;
info.Bounds = r;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.
Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
}
public static bool ClickGridCheckBox
DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus)
{
bool result = false;
if (gridView != null)
{
gridView.ClearSorting();//禁止排序
gridView.PostEditor();
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info;
Point pt = gridView.GridControl.PointToClient(Control.MousePosition);
info = gridView.CalcHitInfo(pt);
if (info.InColumn && info.Column !=
null && info.Column.FieldName == fieldName)
{
for (int i = 0; i < gridView.RowCount; i++)
{
gridView.SetRowCellValue(i, fieldName, !currentStatus);
}
return true;
}
}
return result;
}
2)傳統(tǒng)DataGridView實(shí)現(xiàn)全選操作
  首先在第一列增加一個(gè)CheckBox控件,然后通過相關(guān)的事件,調(diào)整其位置,并相應(yīng)對(duì)應(yīng)的單擊全選操作,初始化代碼如下所示。
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);
}
}
事件實(shí)現(xiàn)了CheckBox重繪調(diào)整,并處理單擊事件,如下所示。
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();
}
包括兩個(gè)界面樣式的詳細(xì)例子的源碼地址如下:
(慧都控件網(wǎng)版權(quán)所有,轉(zhuǎn)載請(qǐng)注明出處,否則追究法律責(zé)任)
標(biāo)簽:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園