</Columns> </asp:GridView> Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Me.grdContent.Attributes.Add("SortExpression", "QuotationID") Me.grdContent.Attributes.Add("SortDirection", "DESC") ViewState("SortExpression") = "QuotationID" ViewState("SortDirection") = SortDirection.Descending GetData() End If End Sub Protected Sub grdContent_Sorting(ByVal serder As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdContent.Sorting If iSortCount = 0 Then Dim sortExpression As String = e.SortExpression.ToString() Dim sortDirection As String = "DESC" If sortExpression = grdContent.Attributes("SortExpression") Then sortDirection = IIf(grdContent.Attributes("SortDirection").ToString = sortDirection, "ASC", "DESC") End If grdContent.Attributes("SortExpression") = sortExpression grdContent.Attributes("SortDirection") = sortDirection ViewState("SortExpression") = sortExpression ViewState("SortDirection") = sortDirection textSort.Text = CStr(CInt(textSort.Text) + 1).ToString() GetData() iSortCount += 1 End If End Sub Protected Sub grdContent_RowCreated(ByVal serder As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdContent.RowCreated If e.Row.RowType = DataControlRowType.Header Then Dim sortColumnIndex As Integer = GetSortColumnIdex() If sortColumnIndex <> -1 Then AddSortImage(sortColumnIndex, e.Row) ' End If End If End Sub Private Function GetSortColumnIdex() As Integer For Each field As DataControlField In grdContent.Columns If field.SortExpression = ViewState("SortExpression").ToString().Trim() Then Return grdContent.Columns.IndexOf(field) End If Next Return -1 End Function Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow) ' Dim lblsort As New Label() Dim sortDirection As String = "DESC" sortDirection = IIf(grdContent.Attributes("SortDirection").ToString = sortDirection, "ASC", "DESC") If sortDirection = "ASC" Then lblsort.Text = "<font color='red'>▲</font>" Else lblsort.Text = "<font color='red'>▼</font>" End If headerRow.Cells(columnIndex).Controls.Add(lblsort) End Sub
private string GridViewSortDirection
{
get { return ViewState["SortDirection"] as string ?? "ASC"; }
set { ViewState["SortDirection"] = value; }
}
//记录排序字段
private string GridViewSortExpression
{
get { return ViewState["SortExpression"] as string ?? string.Empty; }
set { ViewState["SortExpression"] = value; }
}
//取得排序方向(原本升序点了列头后降序排列)
private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;
case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}
//排序表
protected DataView SortDataTable(DataTable dataTable)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
return dataView;
}
else
{
return new DataView();
}
}
//GridView的Sorting事件代理给的方法
protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression = e.SortExpression;
int pageIndex = gridViewPublishers.PageIndex;
gridViewPublishers.DataSource = SortDataTable(dataTable);
gridViewPublishers.DataBind();
gridViewPublishers.PageIndex = pageIndex;
}
<asp:GridView ID="gridViewPublishers" AllowSorting="true" OnSorting="gridViewPublishers_Sorting" runat="server">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<Columns/>
<asp:GridView/>
2 {
3 if (!IsPostBack)
4 {
5 //初始化页面绑定数据到GridView
6 GetDataSet();
7 GridViewBind();
8 }
9 }
10
11 /// <summary>
12 /// 生成ds数据集
13 /// </summary>
14 private DataSet GetDataSet()
15 {
16 //string strSQL = "SELECT * FROM MANAGER.DB1";
17 string strSQL = "SELECT * FROM MANAGER.DB1";
18 DataSet ds = conn.GetDs(strSQL);
19 return ds;
20 }
21
22 /// <summary>
23 /// 绑定数据到GridView
24 /// </summary>
25 private void GridViewBind()
26 {
27 this.GridView1.DataSource = this.GetDataSet().Tables[0].DefaultView;
28 this.GridView1.DataBind();
29 }
30 /// <summary>
31 /// 存储选定列当前排序状态
32 /// </summary>
33 public SortDirection GridViewSortDirection
34 {
35 get
36 {
37 if (ViewState["sortDirection"] == null)
38 ViewState["sortDirection"] = SortDirection.Ascending;
39 return (SortDirection)ViewState["sortDirection"];
40 }
41 set { ViewState["sortDirection"] = value; }
42 }
43
44 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
45 {
46 string sortExpression = e.SortExpression;
47 if (GridViewSortDirection == SortDirection.Ascending)
48 {
49 GridViewSortDirection = SortDirection.Descending;
50 SortGridView(sortExpression, "DESC");
51 }
52 else
53 {
54 GridViewSortDirection = SortDirection.Ascending;
55 SortGridView(sortExpression, "ASC");
56 }
57 }
58
59 /// <summary>
60 /// 排序并绑定
61 /// </summary>
62 private void SortGridView(string sortExpression, string direction)
63 {
64 DataTable dt = this.GetDataSet().Tables[0];
65 DataView dv = new DataView(dt);
66
67 dv.Sort = sortExpression + " " + direction;
68
69 GridView1.DataSource = dv;
70 GridView1.DataBind();
71 }
<asp:GridView ID="GridView2" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" Width="233px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Pleib" HeaderText="Pleib" SortExpression="Pleib" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:xierflyConnectionString %>"
SelectCommand="SELECT * FROM [ProductClass]"></asp:SqlDataSource>
{
GridView1.Sort(e.SortExpression, SortDirection.Descending);
GridView1.DataBind();
}
编译没有问题,运行有问题。我知道问题在于应该给GridView1指定一个DataSource,但是我就是想在当前的GridView1的结果里面执行排序(不想再重新搜索一遍数据库),怎么办呢??
45 {
46 e.SortExpression=SortExpression.Ascending||SortDirection.Descending;
GridView1.DataBind();
47 }
{
GridView1.Sort(e.SortExpression, SortDirection.Descending);
GridView1.DataBind();
}
{
GridView1.Sort(e.SortExpression,SortDirection.Descending);
GridView1.DataBind();
} 这个有运行有错!!!!
{Cannot evaluate expression because the current thread is in a stack overflow state.}
AllowSorting= "True "
SortExpression= "fieldname "
AllowPaging="True" AllowSorting="True" CssClass="GridViewStyle"
PageSize="20" Width="100%" OnSorting= "grdContent_Sorting" OnRowCreated="grdContent_RowCreated">
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
<Columns>
<asp:BoundField HeaderText="QuotationRevisionID" DataField="QuotationRevisionID" Visible="false" />
<asp:TemplateField HeaderText="QuotationID" HeaderStyle-ForeColor ="Black" SortExpression ="QuotationID" >
<ItemTemplate>
<%#Eval("QuotationID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WMModel" HeaderStyle-ForeColor ="Black" SortExpression ="WMModel">
<ItemTemplate>
<%#Eval("InternalModel")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="ClientID" DataField="ClientID" HeaderStyle-ForeColor ="Black" SortExpression ="ClientID"/>
<asp:BoundField HeaderText="ClientModel" DataField="ClientModel" HeaderStyle-ForeColor ="Black" SortExpression ="ClientModel"/>
<asp:BoundField HeaderText="Collection" DataField="Collection" HeaderStyle-ForeColor ="Black" SortExpression ="Collection"/>
<asp:BoundField HeaderText="Revision" DataField="Revision" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:BoundField HeaderText="CreatedDate" DataField="CreatedDate" DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-ForeColor ="Black" SortExpression ="CreatedDate"/>
<asp:BoundField HeaderText="ModifiedDate" DataField="ModifiedDate" DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-ForeColor ="Black" SortExpression ="ModifiedDate"/>
</Columns>
</asp:GridView>
Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.grdContent.Attributes.Add("SortExpression", "QuotationID")
Me.grdContent.Attributes.Add("SortDirection", "DESC")
ViewState("SortExpression") = "QuotationID"
ViewState("SortDirection") = SortDirection.Descending
GetData()
End If
End Sub
Protected Sub grdContent_Sorting(ByVal serder As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdContent.Sorting
If iSortCount = 0 Then
Dim sortExpression As String = e.SortExpression.ToString()
Dim sortDirection As String = "DESC"
If sortExpression = grdContent.Attributes("SortExpression") Then
sortDirection = IIf(grdContent.Attributes("SortDirection").ToString = sortDirection, "ASC", "DESC")
End If
grdContent.Attributes("SortExpression") = sortExpression
grdContent.Attributes("SortDirection") = sortDirection
ViewState("SortExpression") = sortExpression
ViewState("SortDirection") = sortDirection
textSort.Text = CStr(CInt(textSort.Text) + 1).ToString()
GetData()
iSortCount += 1
End If
End Sub Protected Sub grdContent_RowCreated(ByVal serder As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdContent.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
Dim sortColumnIndex As Integer = GetSortColumnIdex()
If sortColumnIndex <> -1 Then
AddSortImage(sortColumnIndex, e.Row) '
End If
End If
End Sub Private Function GetSortColumnIdex() As Integer
For Each field As DataControlField In grdContent.Columns
If field.SortExpression = ViewState("SortExpression").ToString().Trim() Then
Return grdContent.Columns.IndexOf(field)
End If
Next
Return -1
End Function Private Sub AddSortImage(ByVal columnIndex As Integer, ByVal headerRow As GridViewRow) '
Dim lblsort As New Label()
Dim sortDirection As String = "DESC"
sortDirection = IIf(grdContent.Attributes("SortDirection").ToString = sortDirection, "ASC", "DESC")
If sortDirection = "ASC" Then
lblsort.Text = "<font color='red'>▲</font>"
Else
lblsort.Text = "<font color='red'>▼</font>"
End If
headerRow.Cells(columnIndex).Controls.Add(lblsort)
End Sub