要排序需要手动实现GridView的Sorting事件
aspx.cs文件:
public partial class basedata_gridview : System.Web.UI.Page
{
public string strsql = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
////排序//
ViewState["sortExpression"] = "XZ_ID";//表的唯一id
ViewState["sort"] = " DESC";
////排序//
DoDataBind();
}
}
protected void DoDataBind()
{
strsql = "select * from XZ_Table where 1=1";//zsid='" + str_ID + "'";
strsql = strsql + " order by FA_Class";
DataSet ds = new DataSet();
ds = SqlDb.ExecuteDataSet(strsql);//ExecuteDataSet(OracleHelper.ConnectionStringLocalTransaction, CommandType.Text, strsql);
////排序//
DataView myview = ds.Tables[0].DefaultView;
myview.Sort = ViewState["sortExpression"].ToString() + ViewState["sort"].ToString();
////排序////
this.gvDGIList.DataSource = myview;
this.gvDGIList.DataBind();
}
protected void gvDGIList_Sorting(object sender, GridViewSortEventArgs e)
{
////排序//
ViewState["sortExpression"] = e.SortExpression;
if (ViewState["sort"].ToString() == " DESC")
{
ViewState["sort"] = " ASC";
DoDataBind();
}
else
{
ViewState["sort"] = " DESC";
DoDataBind();
}
////排序//
}
}
aspx文件
<asp:GridView ID="gvDGIList" runat="server" AllowSorting="True" AutoGenerateColumns="False" OnSorting="gvDGIList_Sorting">
<Columns>
<asp:TemplateField HeaderText="限制编号" Visible="False" >
<ItemTemplate>
<asp:Label ID="Label_XZ_ID" runat="server" Text='<%# Bind("XZ_ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="方案" >
<ItemTemplate>
<asp:Label ID="Label_FA_Class" runat="server" Text='<%# Bind("FA_Class") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="限制名称" SortExpression="XZ_Name">
<ItemTemplate>
<asp:Label ID="Label_XZ_Name" runat="server" Text='<%# Bind("XZ_Name") %>' ></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="限制类别" >
<ItemTemplate>
<asp:Label ID="Label_XZ_Class" runat="server" Text='<%# Bind("XZ_Class") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>