当前位置:酷百书>生活百科>SortExpression的用法问题

SortExpression的用法问题

2023-09-02 14:55:01 编辑:join 浏览量:612

SortExpression的用法问题

SortExpression的用法问题

排序字段,点击定义了该属性的字段名,可以获得该字段的排序规则,并产生排序事件,修改dataset(数据源)的视图状态,以该字段为规则进行排序

还有同时常用到的属性:SortDirection

下面这个例子可以很好的看出它的用法:

protected void kjkm_dg_SortCommand(object source, DataGridSortCommandEventArgs e)

{

string SortExpression = e.SortExpression.ToString(); //获得当前排序表达式

string SortDirection = "ASC"; //为排序方向变量赋初值

if (SortExpression == kjkm_dg.Attributes["SortExpression"]) //如果为当前排序列

{

SortDirection = (kjkm_dg.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC"); //获得下一次的排序状态

}

kjkm_dg.Attributes["SortExpression"] = SortExpression;

kjkm_dg.Attributes["SortDirection"] = SortDirection;

mikecatbind();

}

GridView.SortExpression 属性 是用来获取与正在排序的列关联的排序表达式。

下面是例子你看看吧。

<%@ Page language="C#" %>

void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)

{

// Use the RowType property to determine whether the

// row being created is the header row.

if (e.Row.RowType == DataControlRowType.Header)

{

// Call the GetSortColumnIndex helper method to determine

// the index of the column being sorted.

int sortColumnIndex = GetSortColumnIndex();

if (sortColumnIndex != -1)

{

// Call the AddSortImage helper method to add

// a sort direction image to the appropriate

// column header.

AddSortImage(sortColumnIndex, e.Row);

}

}

}

// This is a helper method used to determine the index of the

// column being sorted. If no column is being sorted, -1 is returned.

int GetSortColumnIndex()

{

// Iterate through the Columns collection to determine the index

// of the column being sorted.

foreach (DataControlField field in CustomersGridView.Columns)

{

if (field.SortExpression == CustomersGridView.SortExpression)

{

return CustomersGridView.Columns.IndexOf(field);

}

}

return -1;

}

// This is a helper method used to add a sort direction

// image to the header of the column being sorted.

void AddSortImage(int columnIndex, GridViewRow headerRow)

{

// Create the sorting image based on the sort direction.

Image sortImage = new Image();

if (CustomersGridView.SortDirection == SortDirection.Ascending)

{

sortImage.ImageUrl = "~/Images/Ascending.jpg";

sortImage.AlternateText = "Ascending Order";

}

else

{

sortImage.ImageUrl = "~/Images/Descending.jpg";

sortImage.AlternateText = "Descending Order";

}

// Add the image to the appropriate header cell.

headerRow.Cells[columnIndex].Controls.Add(sortImage);

}

GridView AllowSorting Exampledatasourceid="CustomersSource"

autogeneratecolumns="false"

emptydatatext="No data available."

allowsorting="true"

onrowcreated="CustomersGridView_RowCreated"

runat="server">

headertext="Customer ID"

headerstyle-wrap="false"

sortexpression="CustomerID"/>

headertext="CompanyName"

headerstyle-wrap="false"

sortexpression="CompanyName"/>

headertext="Address"

headerstyle-wrap="false"

sortexpression="Address"/>

headertext="City"

headerstyle-wrap="false"

sortexpression="City"/>

headertext="Postal Code"

headerstyle-wrap="false"

sortexpression="PostalCode" />

headertext="Country"

headerstyle-wrap="false"

sortexpression="Country"/>

selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"

connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"

runat="server"/>

标签:SortExpression,用法

版权声明:文章由 酷百书 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.kubaishu.com/life/369879.html
热门文章