Use devexpress gridcontrol

Source: Internet
Author: User
Tags rowcount

Public form2 ()
{
Initializecomponent ();

// Do not display the built-in navigation bar.
Gc1.useembeddednavigator = false;

// Panel with no group displayed
Gv1.optionsview. showgrouppanel = false;
Gv2.optionsview. showgrouppanel = false;

// Automatically change the line Content
Gv1.optionsview. rowautoheight = true;
Gv2.optionsview. rowautoheight = true;

// Allows automatic merge of cells
Gv1.optionsview. allowcellmerge = true;

// If the contents of the slave table are not found in the master-slave table, the table is displayed (not displayed by default)
Gv1.optionsdetail. allowexpandemptydetails = true;

// Display the automatically filtered rows (the effect is similar to that of the automatically filtered rows in Excel)
Gv2.optionsview. showautofilterrow = true;

// Make the gridview uneditable
Gv1.optionsbehavior. editable = false;
Gv2.optionsbehavior. editable = false;

// Built-in editor Display Mode
Gv1.optionsbehavior. editorshowmode = devexpress. utils. editorshowmode. Click;

// Whether the display function of the Master/Slave table is available
// Gv1.optionsdetail. enablemasterviewmode = false;

// If the Master/Slave table is displayed, A tabs is displayed with a plus sign each time. Two panels are usually displayed.
// In fact, this is not necessary. Generally, disable it. The disabled object is the property of the primary gridview.
Gv1.optionsdetail. showdetailtabs = false;


}

Private void btnexit_click (Object sender, eventargs E)
{
This. Close ();

}

Private void btnok_click (Object sender, eventargs E)
{
// Set the connection string
Dbhelpersql. connectionstring = dbhelpersql. createconnectionstring (@ "Buddhas \ sqlexpress", "sa", "00000", "rtdmes ");

Dataset DS = new dataset ();
// Data of the parent gridview
String SQL = "select SC _prno, SC _prna from mespb04h ";
Dbhelpersql. queryd (SQL, DS, "Main ");

// Data of the Child gridview
SQL = "select pa_name, pa_no, SC _prno from mespb09h ";
Dbhelpersql. queryd (SQL, DS, "son ");

// This is the key to displaying Master/Slave tables. 1. gridcontrol analyzes 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 design gridview. Otherwise, the result is 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 the key. It cannot be set to DS directly. The table must be specified.
Gc1.datasource = Ds. Tables ["Main"];
}

The above code shows a master-slave table. Tips for displaying Master/Slave tables. The document has not been mentioned yet. I tried it for one night. As shown above, it is actually very simple.

The preceding dbhelpersql was developed from codematic 2.0 and I modified it myself.

The code above is as follows:

  

  

For the gridview, I have two major questions:

1. The Master/Slave table is displayed.

2. Grouping display, which is frequently used in reports. The gridview solution is simple. You only need to set the groupindex attribute of the corresponding column:

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

Click the fields to be grouped, And the groupindex In the attribute column is set to 0, 1... in turn. The-1 value remains unchanged on fields that do not need to be grouped.

Enter feature Browse/Summary/group Sammary/Summary items and add the fields to be grouped for statistics. Here, we need to set the fieldname field name. Where is the field shown by showingroupcolumnfooter? sammarytype indicates that the calculated field is sum (sum, average, maximum, minimum, etc)

You can set two attributes in grouping/behavior/grneral /.
When autoexpandallgroups is set to true, all groups are expanded, and vice versa.
Whether showgroupedcolumns displays the fields based on your group in the grid, and false indicates that it is not displayed.

Similarly, in feature Browse/Summary/total Sammary, these are not for grouping, but for all rows. Set the columns to be processed in summaryitem, mainly set the column and summarytype of the columns to be processed.


Of course, set the showfooter attribute of optionview to true, and a summary row will appear at the bottom of the table.

Supplement: three other attributes need to be used. The query results are generally read-only and have no focus on each cell. Therefore, you can select all fields and set allowedit to false; allowfocus is false; readonly is true;

Add a row number to the gridview of xtragrid. The sample code is as follows:

// Set the width of the row indicator (the row indicator is the column in the leftmost column that is not displayed)
// By default, if the row number is to be displayed, the width is insufficient and the number is unclear.
// 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 ();
}
}

As follows:

  

Common Code:

// Add a group statistics field
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 grouping statistics fields
(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 borders for controls
Controlpaint. drawborder3d (E. Graphics,
R,
(E. info. State = devexpress. utils. Drawing. objectstate. pressed? Border3dstyle. sunkenouter

: Border3dstyle. raisedinner ));


// Focus cell column
Gridview1.focusedcolumn

// The row number of the row where the focus cell is located
Gridview1.focusedrowhandle

// Focus cell value
Gridview1.focusedvalue

// Specify the string value displayed in the cell
Gridview1.getrowcelldisplaytext (INT rowhandler, string feildname)

// Specify the cell value
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 focus cell value
Gridview1.setfocusedvalue (object value)

// Set the value of the specified Cell
// With this function, you can create a row manually.
Gridview1.setrowcellvalue (INT rowhandler, string feildname, object value)
Gridview1.setrowcellvalue (INT rowhandler, gridcolumn Col, object value)

// Add a new row

Gv1.addnewrow ();
Foreach (gridcolumn Col in gv1.columns)
{
// The method with RowCount-1 is not good
// Gv1.setrowcellvalue (gv1.rowcount-1, Col, "AAAAA ");
Gv1.setrowcellvalue (gv1.focusedrowhandle, Col, "AAAAA ");
}

Gv1.updatecurrentrow ();

// Automatically generates columns Based on the bound Data Source

Gv1.populatecolumns ();

// Add a drop-down list for the column (add 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;

 

From: http://hi.baidu.com/renrsh/blog/item/38a31e458ee33d35cffca3a3.htmls

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.