How to select all headers of the traditional Winform dview and DevExpress controls GridControl (available in source code)

Source: Internet
Author: User

When developing individual projects, it is not convenient and ideal for a customer to reflect the default multiple-select GridView operation. To add a column to the left of the list, select the check box, in addition, it is best to support all the operations in the list header. Otherwise, when there is a large amount of data, you should select one by one.

Based on the above requirements, I have found many examples for comparison and tested and improved the code, and finally completed the above functions. As I have processed multiple sets of interfaces, full selection based on the traditional dview is indispensable, and the full selection of the GridView Based on the DevExpress control should also be supported.

No picture, no truth. The following describes two different effects, and then describes the implementation of the Code in detail.

 

1) Select multiple options for the DevExpress control's GridView

First, let's talk about the implementation of the DevExpress control's GridView. Basically, the function to be implemented is to handle the operations such as clicking select all and re-drawing the table header. First, we need to load the first step to implement related events and operations, as shown below.

This. gridView1.Click + = new System. EventHandler (this. gridview#click );
This. gridView1.CustomDrawColumnHeader + = new DevExpress. XtraGrid. Views. Grid. ColumnHeaderCustomDrawEventHandler (this. gridview1_customdmdrawcolumnheader );
This. gridview1.performancechanged + = new EventHandler (gridview1_performancechanged );

 

Then implement the event operations. The corresponding code is as follows.

Private void gridView1_Click (object sender, EventArgs e)
{
If (DevControlHelper. ClickGridCheckBox (this. gridView1, "Check", m_checkStatus ))
{
M_checkStatus =! M_checkStatus;
}
}

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

 

The operation of clicking and drawing the table header is handed over to another DevControlHelper class for independent processing. The operation of changing the data source gridview1_performancechanged is to find the corresponding all-selected column and set the column width and hide the table header title, and set it to the checkbox style.

The implementation code of 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, string fieldName, bool currentStatus)
{
Bool result = false;
If (gridView! = Null)
{
GridView. ClearSorting (); // disable 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) full selection of the traditional dview

First, add a CheckBox control in the first column, adjust its position through related events, and click Select all corresponding operations. The initialization code is as follows.

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 re-painting 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 (maid Row in dgvSelectAll. Rows)
{
(DataGridViewCheckBoxCell) Row. Cells ["chkBxSelect"]). Value = HCheckBox. Checked;
}
DgvSelectAll. RefreshEdit ();
}

 

The Source Code address of a detailed example that includes two interface styles is as follows: http://files.cnblogs.com/wuhuacong/TestGridViewCheck.rar

Thank you for your reading and support. If you have any suggestions or questions, contact me to discuss and promote them.

 

 

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.