Asp.net GridView multi-column sorting and sorting direction icon code

Source: Internet
Author: User

Asp tutorial. net GridView multi-column sorting and sorting direction icon code

Public class WebGridView: GridView
{
/**/
///
/// Whether to enable or disable multi-column sorting
///
[
Description ("whether to enable multi-column sorting "),
Category ("sort "),
DefaultValue ("false "),
]
Public bool AllowMultiColumnSorting
{
Get
{
Object o = ViewState ["EnableMultiColumnSorting"];
Return (o! = Null? (Bool) o: false );
}
Set
{
AllowSorting = true;
ViewState ["EnableMultiColumnSorting"] = value;
}
}
/**/
///
/// Icon displayed in ascending order
///
[
Description ("icon displayed in ascending order "),
Category ("sort "),
Editor ("System. Web. UI. Design. UrlEditor", typeof (System. Drawing. Design. UITypeEditor )),
DefaultValue (""),

]
Public string SortAscImageUrl
{
Get
{
Object o = ViewState ["SortImageAsc"];
Return (o! = Null? O. ToString ():"");
}
Set
{
ViewState ["SortImageAsc"] = value;
}
}
/**/
///
/// Icon displayed in descending order
///
[
Description ("show icons in descending order "),
Category ("sort "),
Editor ("System. Web. UI. Design. UrlEditor", typeof (System. Drawing. Design. UITypeEditor )),
DefaultValue (""),
]
Public string SortDescImageUrl
{
Get
{
Object o = ViewState ["SortImageDesc"];
Return (o! = Null? O. ToString ():"");
}
Set
{
ViewState ["SortImageDesc"] = value;
}
}
Protected override void OnSorting (GridViewSortEventArgs e)
{
If (AllowMultiColumnSorting)
{
E. SortExpression = GetSortExpression (e );
}

Base. OnSorting (e );
}
Protected override void OnRowCreated (GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. Header)
{
If (SortExpression! = String. Empty)
{
DisplaySortOrderImages (SortExpression, e. Row );
This. CreateRow (0, 0, DataControlRowType. EmptyDataRow, DataControlRowState. Normal );
}
}
Base. OnRowCreated (e );
}
/**/
///
/// Obtain the sort expression
///
Protected string GetSortExpression (GridViewSortEventArgs e)
{
String [] sortColumns = null;
String sortAttribute = SortExpression;

If (sortAttribute! = String. Empty)
{
SortColumns = sortAttribute. Split (",". ToCharArray ());
}
If (sortAttribute. IndexOf (e. SortExpression)> 0 | sortAttribute. StartsWith (e. SortExpression ))
{
SortAttribute = ModifySortExpression (sortColumns, e. SortExpression );
}
Else
{
SortAttribute + = String. Concat (",", e. SortExpression, "ASC ");
}
Return sortAttribute. TrimStart (",". ToCharArray (). TrimEnd (",". ToCharArray ());

}
/**/
///
/// Modify the sorting order
///
Protected string ModifySortExpression (string [] sortColumns, string sortExpression)
{
String ascSortExpression = String. Concat (sortExpression, "ASC ");
String descSortExpression = String. Concat (sortExpression, "DESC ");

For (int I = 0; I <sortColumns. Length; I ++)
{

If (ascSortExpression. Equals (sortColumns [I])
{
SortColumns [I] = descSortExpression;
}

Else if (descSortExpression. Equals (sortColumns [I])
{
Array. Clear (sortColumns, I, 1 );
}
}

Return String. Join (",", sortColumns). Replace (","). TrimStart (",". ToCharArray ());

}
/**/
///
/// Obtain the current expression to sort the selected columns
///
Protected void SearchSortExpression (string [] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
{
SortOrder = "";
SortOrderNo =-1;
For (int I = 0; I <sortColumns. Length; I ++)
{
If (sortColumns [I]. StartsWith (sortColumn ))
{
SortOrderNo = I + 1;
If (AllowMultiColumnSorting)
{
SortOrder = sortColumns [I]. Substring (sortColumn. Length). Trim ();
}
Else
{
SortOrder = (SortDirection = SortDirection. Ascending )? "ASC": "DESC ");
}
}
}
}
/**/
///
/// Draw an image in ascending and descending order
///
Protected void DisplaySortOrderImages (string sortExpression, GridViewRow dgItem)
{
String [] sortColumns = sortExpression. Split (",". ToCharArray ());

For (int I = 0; I <dgItem. Cells. Count; I ++)
{
If (dgItem. Cells [I]. Controls. Count> 0 & dgItem. Cells [I]. Controls [0] is LinkButton)
{
String sortOrder;
Int sortOrderNo;
String column = (LinkButton) dgItem. Cells [I]. Controls [0]). CommandArgument;
SearchSortExpression (sortColumns, column, out sortOrder, out sortOrderNo );
If (sortOrderNo> 0)
{
String sortImgLoc = (sortOrder. Equals ("ASC ")? SortAscImageUrl: SortDescImageUrl );

If (sortImgLoc! = String. Empty)
{
Image imgSortDirection = new Image ();
ImgSortDirection. ImageUrl = sortImgLoc;
DgItem. Cells [I]. Controls. Add (imgSortDirection );

}
Else
{

If (AllowMultiColumnSorting)
{
Literal litSortSeq = new Literal ();
LitSortSeq. Text = sortOrderNo. ToString ();
DgItem. Cells [I]. Controls. Add (litSortSeq );

}
}
}
}
}

}
}

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.