Enables DataGridView and Devexpress.gridcontrol header full-select functions

Source: Internet
Author: User

 1) Implementation of the GridView DevExpress control multi-Select operation

First of all, the implementation of the GridView of the DevExpress control, the function to be implemented is basically to handle clicking the Select all operation, redrawing the table-first operation, first implementing the related events and actions in the first step of loading, as shown below.

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);

Then it is the implementation of the event inside the operation, the corresponding code is shown below.

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 ();
}
}

Where the click and Draw the table header operation, to another class devcontrolhelper to handle independently, the data source changes gridview1_datasourcechanged implementation of the operation is to find the corresponding select all columns, and set the column width, hidden header header, and set to the check box style.

The implementation code for the Devcontrolhelper class is as follows:

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 ();//Prohibit sorting

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) Traditional DataGridView for all-select operation

First, add a CheckBox control in the first column, then adjust its position through the related events, and click the Select all operation accordingly, and the initialization code is shown below.

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);
}
}

The event implements the checkbox redraw adjustment and handles the Click event as shown below.

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 ();
}

Enables DataGridView and Devexpress.gridcontrol header full-select functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.