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

内存

我已经在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>
内存

我在这里可用的线程的帮助下对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;
            }

效果很好,希望对任何人有帮助。谢谢

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ASP.NET Web应用程序中的日期之间进行过滤

从 GridView 获取值以在 asp.net Web 应用程序中进行编辑

如何对ASP.NET Core Web应用程序(.Net Framework)进行单元测试?

在asp.net中对gridview列进行排序

如何在ASP.NET Web应用程序中的服务器和客户端之间进行通信?

如何在ASP.NET C#应用程序中向GridView添加行

如何对ASP.NET MVC Web应用程序进行授权的HttpWebRequest

如何在.NET v3.5中创建ASP.NET Web窗体应用程序

ASP到底如何。NET核心Web应用程序启动

如何获取 ASP .net web 应用程序的虚拟路径?

如何在Web应用程序中创建超链接新的ASP.NET Web表单

如何处理asp.net Web应用程序中的“违反UNIQUE KEY约束”异常

如何在RedHat OS中运行asp.net MVC4 Web应用程序

如何从ASP.NET Core Web应用程序中删除Bower?

如何在 ASP.NET Core Web 应用程序中添加 API

如何在ASP.NET MVC中获取Web应用程序的基本URL?

ASP.NET如何在发布应用程序期间在web.config中设置变量

Azure中的ASP.NET Web应用程序-如何记录错误?

如何在Apache网站中托管ASP.NET Core Web应用程序?

异步在ASP.NET Web应用程序中如何工作?

如何从Asp.Net MVC Web应用程序中的FileStremResult对象获取文件?

如何在Asp.net Web窗体应用程序中的代码后面设置SelectParameter?

如何在ASP.NET Web应用程序中实现这种加载效果

如何在ASP.NET Web应用程序中搜索数据

如何在Asp.Net Web应用程序中测试CSRF攻击?

如何在div中显示(ASP.NET)Web应用程序?

如何使用 asp.net C# 在 Web 应用程序中显示捕获的指纹

如何在 ASP.NET Web 应用程序开发中更高效?

ASP.NET Web应用程序的内存中缓存