Implement bidirectional sorting of the control DataGrid

Source: Internet
Author: User

The DataGrid Server Control provides a way to add the sorting function to the grid. You can use the following methods to sort data:
(1) default sorting: All columns in the grid can be sorted. the header of each column contains a linkbutton control. You can click the sort control to sort the columns. To use default sorting, set the properties of the DataGrid Control to enable sorting of all columns, and then createProgramExecute sort and set the default sort as follows:
1: In the "design" view, select the DataGrid Control and click the "Property generator" link at the bottom of the "properties" window.
2: In the "DataGrid properties" dialog box, click the "General" tab.
3: Set the allowsorting attribute of the grid to true.
4: During running, the headers of all columns are automatically displayed as linkbutton controls. When you click a column header, the name of the bound field in the column is passed as the sort keyword.
(2) custom sorting: defines which columns can be sorted and what type of buttons you click in the column header for sorting.
Custom sorting is to define binding columns and template columns in the grid, and write in the sortcommand eventCodeSort data. You can follow the steps below to set a custom partition:
1: In the "design" view, select the control and click the "Property generator" link at the bottom of the "properties" window.
2: In the "DataGrid properties" dialog box, click the "General" tab.
3: In the "behavior" section, select the "allow sort" box and switch to the "column" tab.
4: add any combination of binding columns and template columns to the control.
5: To enable column sorting, enter a value for the "Sort expression" of the "sort" column. It is usually the name of the data field to be bound to in the values column.

Page code:

<Form ID = "form1" method = "Post" runat = "server">
<H2> bidirectional sorting in the DataGrid </H2>
<Asp: DataGrid id = "dgpagerstate" style = "Z-INDEX: 102; left: 16px; position: absolute; top: 64px"
Runat = "server" bordercolor = "# e7e7ff" borderstyle = "NONE" borderwidth = "1px" backcolor = "white"
Cellpadding = "3" gridlines = "horizontal" pagesize = "5" Height = "20px" autogeneratecolumns = "false"
Width = "376px" allowsorting = "true" allowpaging = "true">
<Selecteditemstyle font-bold = "true" forecolor = "# f7f7f7" backcolor = "# 738a9c"> </selecteditemstyle>
<Alternatingitemstyle backcolor = "# f7f7f7"> </alternatingitemstyle>
<Itemstyle forecolor = "# 4a3c8c" backcolor = "# e7e7ff"> </itemstyle>
<Headerstyle font-bold = "true" forecolor = "# f7f7f7" backcolor = "# 4a3c8c"> <Footerstyle forecolor = "# 4a3c8c" backcolor = "# b5c7de"> </footerstyle>
<Columns>
<Asp: boundcolumn datafield = "lastname" sortexpression = "lastname" headertext = "lastname"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "firstname" sortexpression = "firstname" headertext = "firstname"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "city" sortexpression = "city" headertext = "city"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "birthdate" sortexpression = "birthdate" headertext = "birthdate" dataformatstring = "{0: d}"> </ASP: boundcolumn>
</Columns>
<Pagerstyle horizontalalign = "right" forecolor = "# 4a3c8c" backcolor = "# e7e7ff" mode = "numericpages"> </pagerstyle>
</ASP: DataGrid>
</Form>

Background code:

Page loading events

Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
// Set the sorting expression
Viewstate ["sortorder"] = "lastname ";
// Set the ascending or descending order of sorting
Viewstate ["orderdire"] = "ASC ";
// Data Binding
Datagriddatabind ();
}
}

Data Binding method

Private void datagriddatabind ()
{
// Define the data connection object. The database connection string is defined in the web. config file.
Sqlconnection conn = new sqlconnection (configurationsettings. etettings ["connectionsqlserver"]. tostring ());
// Create a data adapter object
Sqldataadapter da = new sqldataadapter ("select lastname, firstname, title, titleofcourtesy, birthdate, city from employees", Conn );
// Create a DataSet object
Dataset DS = new dataset ();
Try
{
// Fill the dataset
Da. Fill (DS, "testtable ");
// Obtain the default view of the filled table
Dataview view = Ds. Tables ["testtable"]. defaultview;
// Set the sorting keyword
String sort = (string) viewstate ["sortorder"] + "" + (string) viewstate ["orderdire"];
// Set the sorting expression
View. Sort = sort;
// Data Bonding
Dgpagerstate. datasource = view;
Dgpagerstate. databind ();
}
Catch (Exception error)
{
Response. Write (error. tostring ());
}
}

 Page events of the DataGrid Control

Private void dgpagerstate_pageindexchanged (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)
{
// Set the index value of the current displayed page of The DataGrid to the selected page index value
Dgpagerstate. currentpageindex = E. newpageindex;
Datagriddatabind ();
}

Sorting event of the DataGrid

Private void dgpagerstate_sortcommand (Object source, system. Web. UI. webcontrols. datagridsortcommandeventargs E)
{
// Obtain the sort expression
String vortexp = E. sortexpression;
// If it is the current sort expression
If (viewstate ["sortorder"]. tostring () = vortexp)
{
// If the original descending order is changed to ascending order, and vice versa
If (string) viewstate ["orderdire"] = "DESC ")
Viewstate ["orderdire"] = "ASC ";
Else
Viewstate ["orderdire"] = "DESC ";
}
Else
{
// Reset the sorting keyword
Viewstate ["sortorder"] = E. sortexpression;
}
Datagriddatabind ();
}

Click the column headers of the DataGrid to sort the data.

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.