b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

datagridview全选_vb.net datagridview_licensecontrol控件

电脑杂谈  发布时间:2017-05-30 22:06:25  来源:网络整理

在开发一个个人项目的时候,有客户反映默认GridView多选操作不是很方便和理想,想在列表的左边增加一列可以勾选,并且最好支持列表头部全选的操作,否则数据多的时候一个个勾选要到天荒地老。

基于以上需求,找了不少例子进行比较,并对代码进行测试改进,终于完成了以上的功能了, 并且由于我本身做了多套界面的处理,因此,基于传统的DataGridView全选操作不能少,而且基于DevExpress控件的GridView全选操作也应该支持,呵呵。

无图无,下面先上图介绍两种不同的效果,然后在详细介绍代码的实现。

1)DevExpress控件的GridView的实现多选操作

先讲DevExpress控件的GridView的实现,要实现的功能基本上是处理单击全选操作、重新绘制表头等操作,首先在加载第一步实现相关的事件和操作,如下所示。

this.gridView1.Click+=newSystem.EventHandler(this.gridView1_Click);

this.gridView1.CustomDrawColumnHeader+=newDevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(this.gridView1_CustomDrawColumnHeader);

this.gridView1.DataSourceChanged+=newEventHandler(gridView1_DataSourceChanged);

然后就是实现里面的事件操作了,对应的代码如下所示。

privatevoidgridView1_Click(objectsender,EventArgse)

{

if(DevControlHelper.ClickGridCheckBox(this.gridView1,"Check",m_checkStatus))

{

m_checkStatus=!m_checkStatus;

}

}

privatevoidgridView1_CustomDrawColumnHeader(objectsender,DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgse)

{

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;

}

}

voidgridView1_DataSourceChanged(objectsender,EventArgse)

{

GridColumncolumn=this.gridView1.Columns.ColumnByFieldName("Check");

if(column!=null)

{

column.Width=80;

column.OptionsColumn.ShowCaption=false;

column.ColumnEdit=newRepositoryItemCheckEdit();

}

}

其中单击和绘制表头的操作,交给另外一个类DevControlHelper来独立进行处理,数据源变化gridView1_DataSourceChanged实现的操作是寻找对应的全选列,并设置列宽、隐藏表头标题,并设置为复选框样式。datagridview全选

DevControlHelper类的实现代码如下所示:

publicstaticvoidDrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgse,boolchk)

datagridview全选_licensecontrol控件_vb.net datagridview

{

RepositoryItemCheckEditrepositoryCheck=e.Column.ColumnEditasRepositoryItemCheckEdit;

if(repositoryCheck!=null)

{

Graphicsg=e.Graphics;

Rectangler=e.Bounds;

DevExpress.XtraEditors.ViewInfo.CheckEditViewInfoinfo;

DevExpress.XtraEditors.Drawing.CheckEditPainterpainter;

DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgsargs;

info=repositoryCheck.CreateViewInfo()asDevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;

painter=repositoryCheck.CreatePainter()asDevExpress.XtraEditors.Drawing.CheckEditPainter;

info.EditValue=chk;

info.Bounds=r;

info.CalcViewInfo(g);

args=newDevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info,newDevExpress.Utils.Drawing.GraphicsCache(g),r);

painter.Draw(args);

args.Cache.Dispose();

}

}

publicstaticboolClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridViewgridView,stringfieldName,boolcurrentStatus)

{

boolresult=false;

if(gridView!=null)

{

gridView.ClearSorting();//禁止排序

gridView.PostEditor();

DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfoinfo;

Pointpt=gridView.GridControl.PointToClient(Control.MousePosition);

info=gridView.CalcHitInfo(pt);

if(info.InColumn&&info.Column!=null&&info.Column.FieldName==fieldName)

{

for(inti=0;i<gridView.RowCount;i++)

{

gridView.SetRowCellValue(i,fieldName,!currentStatus);

}

returntrue;

}

}

returnresult;

}

2)传统DataGridView实现全选操作

vb.net datagridview_licensecontrol控件_datagridview全选

首先在第一列增加一个CheckBox控件,然后通过相关的事件,调整其位置,并相应对应的单击全选操作,初始化代码如下所示。datagridview全选

CheckBoxHeaderCheckBox=null;

publicFrmNormalGridViewSelect()

{

InitializeComponent();

if(!this.DesignMode)

{

HeaderCheckBox=newCheckBox();

HeaderCheckBox.Size=newSize(15,15);

this.dgvSelectAll.Controls.Add(HeaderCheckBox);

HeaderCheckBox.KeyUp+=newKeyEventHandler(HeaderCheckBox_KeyUp);

HeaderCheckBox.MouseClick+=newMouseEventHandler(HeaderCheckBox_MouseClick);

dgvSelectAll.CurrentCellDirtyStateChanged+=newEventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);

dgvSelectAll.CellPainting+=newDataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);

}

}

事件实现了CheckBox重绘调整,并处理单击事件,如下所示。

privatevoidHeaderCheckBox_MouseClick(objectsender,MouseEventArgse)

{

HeaderCheckBoxClick((CheckBox)sender);

}

privatevoiddgvSelectAll_CellPainting(objectsender,DataGridViewCellPaintingEventArgse)

{

if(e.RowIndex==-1&&e.ColumnIndex==0)

ResetHeaderCheckBoxLocation(e.ColumnIndex,e.RowIndex);

}

privatevoidResetHeaderCheckBoxLocation(intColumnIndex,intRowIndex)

{

RectangleoRectangle=this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex,RowIndex,true);

PointoPoint=newPoint();

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;

}

privatevoidHeaderCheckBoxClick(CheckBoxHCheckBox)

{

foreach(DataGridViewRowRowindgvSelectAll.Rows)

{

((DataGridViewCheckBoxCell)Row.Cells["chkBxSelect"]).Value=HCheckBox.Checked;

}

dgvSelectAll.RefreshEdit();

}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50185-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...