DevExpress use of GridControl and how to add columns and bind data source

Scenes

Winform controls -DevExpress18 download and install the registration and use in VS:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243

After the above DevExpress build a good environment, to use its GridControl control.

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
public concern number
overbearing program ape
acquisition-related programming e-books, tutorials and free downloads push

achieve

First, drag a GridControl in the form

 

 

Then in the Load event of the form when adding columns and its style settings

Private  void FrmSearch_Load ( Object SENDER, EventArgs E) 
        { 
            // set GridControl style 
            Common.GridControl.GridControlHelper.SetStyles ( the this .gridControl1.MainView AS DevExpress.XtraGrid.Views.Base.ColumnView);
             // subscribe row click event 
            the this .gridView1 + = .RowClick gridView1_RowClick; 

        }

 

Enter styled method

public static void SetStyles(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;

                gridView.OptionsView.ShowGroupPanel = false;                                              //隐藏最上面的GroupPanel
                gridView.OptionsView.ShowIndicator = false;                                               //隐藏指示列

                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;            // set the focus to the entire frame line 
                gridView.OptionsSelection.EnableAppearanceFocusedCell = to false ;                                   // disable cell focus 
                gridView.OptionsSelection.EnableAppearanceFocusedRow = to true ;                                     // enable full line focus 
                gridView.OptionsSelection.EnableAppearanceFocusedRow = to true ;                                     // enable full line focus 
                gridView.OptionsSelection.EnableAppearanceHideSelection = to false ;

                gridView.OptionsView.EnableAppearanceEvenRow = to true ;                                             // enable even rows background color 
                gridView.OptionsView.EnableAppearanceOddRow = to true ;                                              // enable background color of odd rows 

                // gridView.Appearance.EvenRow.BackColor the System.Drawing.Color.FromArgb = (150, 237, 243, 254);       // set the background color even rows
                 // gridView.Appearance.OddRow.BackColor the System.Drawing.Color.FromArgb = (150, 199, 237, 204);        // set the background color of odd rows
                 // gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red;
                // gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.Red; 

            } 

            // Disable automatic generation column 
            view.OptionsBehavior.AutoPopulateColumns = to false ;
             // Disable automatic column width 
            IF (View IS DevExpress.XtraGrid.Views.Grid .GridView) 
            { 
                (View AS DevExpress.XtraGrid.Views.Grid.GridView) = .OptionsView.ColumnAutoWidth to false ; 
            } 
            // disable data filtering panel 
            view.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never; 

            # region to add columns

            view.Columns.Clear();

            int index = 0;
            DevExpress.XtraGrid.Columns.GridColumn col = null;

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "DBName";
            col.Caption = "数据库名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "ShortNodeText";
            col.Caption = "文件名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "CreateDate";
            col.Caption = "创建日期";
            col.Width = 130;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "TaskFile";
            col.Caption = "任务文件";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "FullPath";
            col.Caption = "完整路径";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "Barcode";
            col.Caption = "电池条码";
            col.Width= 180 [ ; 
            col.VisibleIndex = index ++ ; 
            view.Columns.Add (COL); 

            #endregion 

            SetAllowEdit (View, to false );                                           // Disable editing 
            SetAllowSort (View, DevExpress.Utils.DefaultBoolean.False);           // Disable sort 
            SetAllowFilter ( View, to false );                                         // disable data filtering 
        }

 

Setting and adding columns in the style of the above process

Note FieldName property to set the same field and in the future when the data source when adding a column.

Then the above method editing disabled

public static void SetAllowEdit(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowEdit = isAllow;
            }
        }

 

Disabling sorting method

public static void SetAllowSort(DevExpress.XtraGrid.Views.Base.ColumnView view, DevExpress.Utils.DefaultBoolean value)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowSort = value;
            }
        }

 

Disable data filtering method

 public static void SetAllowFilter(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsFilter.AllowAutoFilter = isAllow;
                col.OptionsFilter.AllowFilter = isAllow;
            }
        }

 

Necessary to set the data source is added after initialization finished style and columns

First create a solid object, the object must be added when the above corresponding columns FieldName property.

The following is a sector fields and properties, other omitted

 

public  class DataTreeNode 
    { 
        Private  String ID;
         Private  String the parentId;
         Private  String nodeText;
         Private  String createDate;
         Private  String fullPath;
         Private  String taskfile;
         Private  String Barcode;
         Private DataTreeNodeTypes nodeType = DataTreeNodeTypes.Folder; 

        ///  <Summary> 
        /// removed data file full path extension
         ///  </ the Summary> 
        public  String Id
        { 
            GET { return ID;}
             SET {ID = value;} 
        } 
        ///  <Summary> 
        /// Id parent node
         ///  </ Summary> 
        public  String the ParentId 
        { 
            GET { return the parentId;}
             SET {= the parentId value;} 
        } 
        ///  <Summary> 
        /// data file name
         ///  </ Summary> 
        public  String NodeText 
        { 
            GET { return nodeText; }
            set { nodeText = value; }
        }
     }

 

Build a data source

List<DataTreeNode> data = new List<DataTreeNode>();
data = DataTreeListHelper.ParseDir(Common.Global.AppConfig.TestDataDir, data);
var result = data.Where(p => p.NodeType = = DataTreeNodeTypes.File);

 

First, the statement above solid object List, and then use the file directory ParseDir method recursive queries.

Then filter out file types.

Then you may be provided directly to the data source

this.gridControl1.DataSource = result;

 

 

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/12065791.html