DevExpress GridControl function Summary

Source: Internet
Author: User

The Dev control has been written before and has been very powerful for a long time. As of now, the Dev version I have compiled has reached 14.1, And the Demo is really powerful, the results are also very good. Based on your own development experience this month, I would like to share my experience after research, so that you will not be able to take more detours.

  • DevExpress installation sequence
  • GridControl basic style settings
  • Common GridControl events and descriptions
  • GridControl right-click menu
  • Select All and invert GridControl
  • Add subtotal in GridControl
  • GridControl Fixed Header

    1. installation has never been so simple

    The Dev installation file has been zoomed in to Baidu cloud for easy download: DevExpress 12.2.7. After downloading, install it in sequence according to the image numbers. This is Dev 12.2.7, which you deserve.

    If (bandedGridView1.GetFocusedDataRow () = null) return; // waybill number var columnValue = bandedGridView1.GetFocusedRowCellValue ("Bind Column field name"). ToString ();

     

     

     

    Note: It is best to use VS2012 and 2010 together with DevExpress 12.2.7. Currently, the toolbox cannot be imported to VS2013.

    2. Do you want to set the basic style?

    When GridControl is used for the first time, why is there a pile of GridView under GridControl? Later I Thought About It. GridControl is a large container, and there are many views below, that is, the Operation view. Below are some of my frequently used style settings.

BandedGridView1.IndicatorWidth = 40; // The width of the auto-increment column bandedGridView1.OptionsView. columnAutoWidth = false; // automatically adjust the column width so that the width of all columns matches the width of the view with bandedGridView1.OptionsCustomization. allowSort = false; // disable bandedGridView1.OptionsCustomization for sorting data. allowColumnResizing = false; // disable the wide bandedGridView1.VertScrollVisibility of each column header = ScrollVisibility. auto; // display the vertical scroll bar bandedGridView1.HorzScrollVisibility = ScrollVisibility. auto; // display the horizontal scroll bar bandedGridView1.OptionsMenu. enableColumnMenu = false; // disable bandedGridView1.OptionsMenu. enableFooterMenu = false; // disable bandedGridView1.OptionsMenu from the footer. enableGroupPanelMenu = false; // disable bandedGridView1.OptionsNavigation from the Group panel. useTabKey = false; // do not use TAB/SHIFT + TAB to move the focus bandedGridView1.OptionsBehavior. editable = false; // You Cannot edit bandedGridView1.OptionsBehavior. readOnly = true; // read-only

 3. You will frequently use these events

CustomDrawRowIndicator: DGV displays the auto-increment row number

            e.Appearance.TextOptions.HAlignment = HorzAlignment.Far;            if (e.Info.IsRowIndicator)            {                if (e.RowHandle >= 0)                {                    e.Info.DisplayText = (e.RowHandle + 1).ToString(CultureInfo.InvariantCulture);                }                else if (e.RowHandle < 0 && e.RowHandle > -1000)                {                    e.Info.Appearance.BackColor = Color.AntiqueWhite;                    e.Info.DisplayText = "G" + e.RowHandle;                }            }

RowCellStyle: The color of the GridView row.

BandedGridView1.Appearance. oddRow. backColor = Color. white; // set the color of an odd number of rows. // if the color is White by default, bandedGridView1.OptionsView can be omitted. enableAppearanceOddRow = true; // enable // use the valid bandedGridView1.Appearance when binding with and above. evenRow. backColor = Color. fromArgb (255,250,205); // set the color of the even row bandedGridView1.OptionsView. enableAppearanceEvenRow = true; // enable // use valid if (e. rowHandle = bandedGridView1.FocusedRowHandle) {e. appearance. font = new Font ("", 9, FontStyle. bold );}View Code

FocusedRowChanged: select a row to change the bound row data to the corresponding control.

If (distinct () = null) return; // determines whether the selected row is null // return value var columnValue = bandedGridView1.GetFocusedRowCellValue ("Name of the bound column field"). ToString ();

4. Add a very common function of right-click menu

Add the ContextMenuStrip control and customize the button. You can find some small 16*16 icons to embellish it.

In this way, a shortcut menu is successfully bound.

5. GridControl select all and reselect

This is a relatively difficult problem. Here I summarize the source code of an entity class with the help of the materials found on the Internet.

 

// Actions // All Rights Reserved, Copyright (C) 2014, ZTO, Ltd. // initialize using System. drawing; using System. windows. forms; using DevExpress. xtraEditors. repository; namespace ZTO. wayBill. utilities {// <summary> // Dev GridControl create all check boxes ////// Modify the record ///// 2014-5-30 version: 1.0 YangHengLian creates a primary key. Pay attention to the sorting of namespaces. ///// Version: 1.0 ///// <author> /// <name> YangHengLian </name> /// <date> </date> /// </author>/ /// </summary> public class DevControlHelper {// <summary> // create a check box /// </summary> /// <param name = "e"> </param> // <param name = "chk"> </param> 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. drawin G. 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 () ;}/// <summary> // select all, invert select /// </summary> /// <param name = "gridView"> </param> /// <param name = "fieldName"> </param> // /<param name = "currentStatus"> </para M> /// <returns> </returns> public static bool ClickGridCheckBox (DevExpress. xtraGrid. views. grid. gridView gridView, string fieldName, bool currentStatus) {bool result = false; if (gridView! = Null) {gridView. clearSorting (); // disable sorting the 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 ;}}}View Code

 

 

The procedure is as follows:

Add code for form loading events

Private bool _ mCheckStatus; // select all GridControl as the global variable. The default value is false: private void FrmInputBill_Load (object sender, EventArgs e) {bandedGridView1.OptionsBehavior. editable = false; bandedGridView1.OptionsBehavior. readOnly = true; var col = new BandedGridColumn {FieldName = "Check", Visible = true, VisibleIndex = 0, ColumnEdit = new RepositoryItemCheckEdit ()}; col. optionsColumn. allowEdit = true; // CheckBox can Edit and change gridBand1.Columns. insert (0, col); bandedGridView1.Click + = bandedGridView1_Click; bandedGridView1.CustomDrawColumnHeader + = response; bandedgridview1.cecechanged + = bandedgridview1_performancechanged; // bind the data source here} # region GridControl supports all selected event private void bandedGridView1_Click (object sender, EventArgs e) {if (DevControlHelper. clickGridCheckBox (this. bandedGridVi Ew1, "Check", _ mCheckStatus) {_ mCheckStatus =! _ MCheckStatus ;}} private void bandedgridview1_mdrawcolumnheader (object sender, ColumnHeaderCustomDrawEventArgs e) {if (e. Column! = Null & e. column. fieldName = "Check") {e. info. innerElements. clear (); e. painter. drawObject (e. info); DevControlHelper. drawCheckBox (e, _ mCheckStatus); e. handled = true ;}} private void bandedgridviewperformancechanged (object sender, EventArgs e) {GridColumn column = this. bandedGridView1.Columns. columnByFieldName ("Check"); if (column! = Null) {column. Width = 40; column. OptionsColumn. ShowCaption = false; column. ColumnEdit = new RepositoryItemCheckEdit () ;}# endregionSelect All and reselect

 

6. Add Subtotal
Step 1

Step 2
Bind after loading data

BandedGridView1.Columns ["bind field column name"]. summary. add (DevExpress. data. summaryItemType. count, "BILL_CODE", "Total: {0}"); // Add a subtotal function to add a statistical object to the specified Column
The code of the delete action to modify the corresponding subtotal is as follows:
BandedGridView1.Columns ["bind field column name"]. Summary [0]. DisplayFormat = string. Format ("subtotal {0}", bandedGridView1.RowCount );
7. Fixed the header


I recorded some common GridControl code and shared it with you. If you are interested, you can ask questions in my group, including not only GridControl, but also other controls. I have also studied and applied them to projects.

In the future, you will be grateful for your hard work.

Related Article

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.