如何在ASP.net Web应用程序中对GridView列进行排序

时间:2016-12-09 12:31:43

标签: javascript c# asp.net sorting gridview

我在我的Web应用程序中使用母版页创建了Web表单。我创建了GridView,我想对列进行排序。我的gridview设计如下:

On

1 个答案:

答案 0 :(得分:0)

我在Gridview中使用可用线程here

对列进行了排序

我的项目编码如下,

<asp:GridView ID="GvSalesItems" runat="server"  AutoGenerateColumns="false" DataKeyNames="sno" CellPadding="4" ForeColor="#333333" GridLines="None"  PageSize="10" AllowPaging="True" AllowSorting="True" onsorting="GridView1_Sorting" EnableViewState="true">
           <Columns>
       <asp:TemplateField>

    <ItemTemplate>
      <asp:CheckBox ID="chk" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>

              <asp:BoundField DataField="sno" HeaderText="Sno"  SortExpression="sno" />
            <asp:BoundField DataField="itemcode" HeaderText="Item Code"  SortExpression="itemcode" />
            <asp:BoundField DataField="itemname" HeaderText="Item Name"  SortExpression="itemname" />
            <asp:BoundField DataField="unit" HeaderText="Unit"  SortExpression="unit" />
            <asp:BoundField DataField="price" HeaderText="Price"  SortExpression="price" />
            <asp:BoundField DataField="default_qty" HeaderText="Qty"  ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"  />
            <asp:BoundField DataField="price" HeaderText="Amount" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"  />


        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

背后的代码是,

 public void GetSalesItems() //Binding GridView
        {
                DataSet ds1 = new DataSet();
                D.id = 2;//SELECT * from DBTABLE
                ds1 = C.DATA1(D);
                GvSalesItems.DataSource = ds1.Tables[0];
                GvSalesItems.DataBind();

        }

使用ASP.net Buildin排序函数对GridView进行排序的编码,

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
            {

                DataSet ds = new DataSet();
                B.id = 2;
                ds = A.DATA(B);
                GvUser.DataSource = ds.Tables[0];
                GvUser.DataBind();

                if (ds.Tables[0] != null)
                {
                    DataView dataView = new DataView(ds.Tables[0]);
                    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                    GvUser.DataSource = dataView;
                    GvUser.DataBind();
                }
            }

            private string GridViewSortDirection
            {
                get { return ViewState["SortDirection"] as string ?? "DESC"; }
                set { ViewState["SortDirection"] = value; }
            }

            private string ConvertSortDirectionToSql(SortDirection sortDirection)
            {
                switch (GridViewSortDirection)
                {
                    case "ASC":
                        GridViewSortDirection = "DESC";
                        break;

                    case "DESC":
                        GridViewSortDirection = "ASC";
                        break;
                }

                return GridViewSortDirection;
            }

它完美无缺。如果有人需要,我希望它会有所帮助。谢谢你