Devexpress GridControl use

// Do not display the built-in navigation bar.
            gc1.UseEmbeddedNavigator = false;

            //Do not display grouped panels
            gv1.OptionsView.ShowGroupPanel = false;
            gv2.OptionsView.ShowGroupPanel = false;

            //Automatically change the row height to adapt to the content
            gv1.OptionsView.RowAutoHeight = true;
            gv2.OptionsView.RowAutoHeight = true;

            //Allow automatic merging of cells
            gv1.OptionsView.AllowCellMerge = true;

            //If the content of the slave table is not found in the master-slave table, it will also be displayed (the default is not displayed)
            gv1.OptionsDetail.AllowExpandEmptyDetails = true;

            //Display automatic filter row (the effect is similar to Excel's automatic filter)
            gv2.OptionsView.ShowAutoFilterRow = true;

            //Make GridView not editable
            gv1.OptionsBehavior.Editable = false;
            gv2.OptionsBehavior.Editable = false;

            //The built-in editor display mode
            gv1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;

            //Whether the function displayed by the master-slave table is available
            //gv1.OptionsDetail.EnableMasterViewMode = false;

            //If the master-slave table is displayed, each time a plus sign is opened, a Tabs will be displayed, and two panels are often displayed in it.
            // Actually this is not necessary. Generally choose to close. The closed object is this property of the main GridView.
            gv1.OptionsDetail.ShowDetailTabs = false;

            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //设置连接字符串
            DbHelperSQL.ConnectionString = DbHelperSQL.CreateConnectionString(@"BUDDHAS\SQLEXPRESS", "sa", "00000", "RTDMES");

            DataSet ds = new DataSet();
            //父GridView的数据
            string sql = "select sc_prno,sc_prna from mespb04h";
            DbHelperSQL.QueryD(sql,ds,"main");

            //子GridView的数据
            sql = "select pa_name,pa_no,sc_prno from mespb09h";
            DbHelperSQL.QueryD(sql,ds,"son");

            //This is the key to display the master-slave table, 1. GridControl analyzes the data by checking the content of DataSet.Relations
            // 2. The key name must be the same as the level name of the hierarchical relationship of the GridView design, otherwise, the result will be unexpected.
            DataRelation relation = new DataRelation("aa", 
                                                     ds.Tables["main"].Columns["sc_prno"], 
                                                     ds.Tables["son"].Columns["sc_prno"]);             ds.Relations.Add(relation );
            

            //This is also a key, it cannot be set directly to: ds, it must be specified to the table.
            gc1.DataSource = ds.Tables["main"];
        }

  The above code is to display a master-slave table. Displaying master-slave tables is a bit tricky. The documentation hasn't said it yet, I figured it out after groping for a night. As you can see above, it's actually quite simple.

  The above DbHelperSQL was made by me from CodeMatic 2.0, and I modified it myself.

  The effect diagram of the above code is as follows:

  

  

  For GridView, I have two big questions:

  1. Display the master-slave table, this is solved.

  Second, group display, which is often used in reports, GridView's solution is very simple, just set the GroupIndex property of the corresponding column to become:

    Right-click on the gridcontrol and select run designer to enter design mode. Click retrieve fields in the columns column to import all the fields in the data source.

 Click on the field that needs to be grouped, and set the groupindex in its attribute column to 0, 1.. in turn, and keep -1 unchanged on the field that does not need grouping.

 Enter feature browse/summary/group sammary/summary items to add fields to be grouped for statistics. Here you need to set its fieldname field name, showingroupcolumnfooter which field is displayed, and sammarytype shows that the field of statistics after grouping is sum (summation, average, maximum value, minimum value, etc.)

 When grouping /behavior /grneral / can set two attributes
autoexpandallgroups to true, all groups will be expanded, and vice versa.
 Whether showgroupedcolumns is in the grid is to display which fields you group by, false is not to display

   the same in feature browse/summary/total sammary These are not for grouping, but for all rows. Set the column to be processed in summaryitem, mainly set the column, summarytype of the column to be processed.


Of course, after setting it up, set the showfooter property of optionview to true, and a summary row will appear at the bottom of the table.

  Supplement: There are three other properties that need to be used. The results of our query are generally read-only, and it is best to have no focus on each cell, so you can select all fields at the same time, set allowedit to false; allowfocus to false; readonly to true;

Add row numbers to the GridView of the XtraGrid. The sample code is as follows:

        //Set the width of the line indicator (the line indicator is the column that displays nothing in the leftmost column)
        //By default, if you want to display the line number in it, the width will be insufficient, and the number display will not be clear
        //So To change the width
        gv1.IndicatorWidth = 20;

        private void gv1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
            }
        }

  The effect diagram is as follows:

  

  Common code:

//添加分组统计字段
gridView1.GroupSummary.Add(SummaryItemType.Count, "Product Name", gridView1.Columns["Product Name"]);
gridView1.GroupSummary.Add(SummaryItemType.Average, "Unit Price", gridView1.Columns["Unit Price"]);

//Set the display format of the group statistics field
((GridSummaryItem)gridView1.GroupSummary[gridView1.GroupSummary.Count - 1]).DisplayFormat = "AVR={0:c}";

//Set the grouping field
gridView1.Columns["Discontinued"].GroupIndex = 0;

//Open all groups
gridView1.ExpandAllGroups();

//Draw a border for the control
ControlPaint.DrawBorder3D(e.Graphics,
                          r, 
                          (e.Info.State == DevExpress.Utils.Drawing.ObjectState.Pressed ? Border3DStyle.SunkenOuter

: Border3DStyle.RaisedInner));


//The column gridview1.FocusedColumn of the focus cell

//The row number of the row where the focus cell is located
gridview1.FocusedRowHandle

//The value of the focus cell
gridview1.FocusedValue

//Specify the string value displayed by the cell
gridview1.GetRowCellDisplayText(int rowHandler,string feildName)

//Specify the value of the cell
gridview1.GetRowCellValue(int rowHandler,string feildName)

//Set the value of the specified column in the row where the focus cell is located
gridview1.SetFocusedRowCellValue(GridColumn col,object value)

//Set the value of the focus cell
gridview1.SetFocusedValue(object value)

//Set the value of the specified cell
//With this function, you can manually create the row.
gridview1.SetRowCellValue(int rowHandler,string feildName,object value)
gridview1.SetRowCellValue(int rowHandler,GridColumn col,object value)

// method to add a new row

gv1.AddNewRow();
foreach (GridColumn col in gv1.Columns)
{
  //The method of using RowCount-1 will not work//gv1.SetRowCellValue(gv1.RowCount-1
  , col, "aaaaa");
  gv1.SetRowCellValue( gv1.FocusedRowHandle, col, "aaaaa");
}

gv1.UpdateCurrentRow();

// Automatically generate columns according to the bound data source

gv1.PopulateColumns();

//Add a drop-down list for the column (same for other types such as date and UpDown)
RepositoryItemLookUpEdit ri = new RepositoryItemLookUpEdit();
//ri.PopupWidth = 200;
ri.DisplayMember = "sc_prna";
ri.ValueMember = "sc_prno";
DataTable dt1 = DbHelperSQL.QueryT("select sc_prno,sc_prna from mespb04h");
ri.DataSource = dt1;
gv1.Columns["sc_prno"].ColumnEdit = ri;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325142952&siteId=291194637
Recommended