基于gridview的双头排序列(Sort columns based on gridview do

2019-10-30 00:44发布

我有一个需求量的地方在我需要我的GridView的列进行排序。 但美中不足的是,我需要把我下面的gridview的列的附加行这将有升序和降序排序图像。 当此图片点击,所选列的排序应该发生。

请指导我!

让我知道如果您有任何疑问/疑虑

谢谢!

Answer 1:

我的决定:

<asp:GridView runat="server" ID="GridViewTest" DataSourceID="CustomersSource" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>         
            <HeaderTemplate>
                <asp:Panel runat="server" BorderWidth="1">
                    <asp:Label runat="server" Text='column {CustomerID}'></asp:Label>
                </asp:Panel>
                <asp:Panel runat="server">
                    <asp:ImageButton runat="server" AlternateText="asc" CommandName="CustomerID" CommandArgument="<%# CommandArgumentAsc %>" OnClick="ImageButton_Click" />
                    <asp:ImageButton runat="server" AlternateText="desc" CommandName="CustomerID" CommandArgument="<%# CommandArgumentDesc %>" OnClick="ImageButton_Click" />
                </asp:Panel>
            </HeaderTemplate>
            <ItemTemplate>
                <%# Eval("CustomerID")%>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>         
            <HeaderTemplate>
                <asp:Panel runat="server" BorderWidth="1">
                    <asp:Label runat="server" Text='column {CompanyName}'></asp:Label>
                </asp:Panel>
                <asp:Panel runat="server">
                    <asp:ImageButton runat="server" AlternateText="asc" CommandName="CompanyName" CommandArgument="<%# CommandArgumentAsc %>" OnClick="ImageButton_Click" />
                    <asp:ImageButton runat="server" AlternateText="desc" CommandName="CompanyName" CommandArgument="<%# CommandArgumentDesc %>" OnClick="ImageButton_Click" />
                </asp:Panel>
            </HeaderTemplate>
            <ItemTemplate>
                <%# Eval("CompanyName")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

<asp:sqldatasource id="CustomersSource"
    selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
    connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
    runat="server"/>

后面的代码:

  protected const string CommandArgumentAsc = "asc";
  protected const string CommandArgumentDesc = "desc";

  protected void ImageButton_Click(object sender, ImageClickEventArgs e)
  {
    var imageButton = sender as ImageButton;

    if (imageButton != null)
    {
      if (imageButton.CommandArgument == CommandArgumentAsc)
      {
        GridViewTest.Sort(imageButton.CommandName, SortDirection.Ascending);
      }

      if (imageButton.CommandArgument == CommandArgumentDesc)
      {
        GridViewTest.Sort(imageButton.CommandName, SortDirection.Descending);
      }
    }
  }


文章来源: Sort columns based on gridview double header