Sort columns based on gridview double header

会有一股神秘感。 提交于 2019-12-25 01:43:51

问题


I have a requirment where in i need to sort the columns of my gridview. But the catch is that i need to place an additional row below my gridview columns which would have ascending and descending sort images. When clicking on this images, sorting of the selected column should take place.

Please guide me!!

Let me know if you have any query/doubts

Thanks!


回答1:


My decision:

<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"/>

Code behind:

  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);
      }
    }
  }


来源:https://stackoverflow.com/questions/6881774/sort-columns-based-on-gridview-double-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!