【问题标题】:How to Sort the GridView column in ASP.net Web Application如何在 ASP.net Web 应用程序中对 GridView 列进行排序
【发布时间】:2017-04-24 22:35:10
【问题描述】:

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

<div id="GridPopUp" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"  DataKeyNames="sno"  CssClass = "grid" >
    <Columns>
        <asp:TemplateField  ItemStyle-Width="200px">
            <ItemTemplate>
                <asp:CheckBox  ID="cbCheck" runat="server"/>
            </ItemTemplate>
            </asp:TemplateField>

        <asp:BoundField ItemStyle-Width="200px" DataField="sno" HeaderText="sno"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="itemcode" HeaderText="ItemCode" />
        <asp:BoundField ItemStyle-Width="200px" IDataField="itemname" HeaderText="ItemName"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="unit" HeaderText="Unit"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="price" HeaderText="Price"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="qty" HeaderText="Qty"  />
         <asp:TemplateField ItemStyle-Width="200px" >
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>
</div>

【问题讨论】:

  • 这对您有帮助吗? stackoverflow.com/a/10498012/1166719
  • 只在gridview上启用排序
  • 我已经这样做了alloworting=true;但它显示错误消息
  • 我已经这样做了 AllowSorting="True"。但它显示错误消息“未处理的 GridView 'GridView1' 触发事件排序。”我定义了 SortExpression="sno" 等等。
  • 感谢您的回复 Pierre-Loup Pagniez 和 fnostro。你建议的线程对我有用。我想在 javascrip 和 jquery 的帮助下在没有回发的情况下做到这一点

标签: javascript c# asp.net sorting gridview


【解决方案1】:

我在可用线程here 的帮助下对 Gridview 中的一列进行了排序

我的项目编码如下,

<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 内置排序函数对 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;
            }

完美运行。如果有人需要,我希望它会有所帮助。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2017-12-14
    • 2011-04-07
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多