【问题标题】:Sort columns based on gridview double header基于gridview双标题对列进行排序
【发布时间】:2011-07-30 07:26:12
【问题描述】:

我有一个要求,我需要对我的 gridview 的列进行排序。但问题是我需要在我的 gridview 列下方放置一个额外的行,这些列将具有升序和降序排序图像。单击此图像时,应对所选列进行排序。

请指导我!!

如果您有任何疑问/疑问,请告诉我

谢谢!

【问题讨论】:

    标签: .net asp.net visual-studio sorting gridview


    【解决方案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);
          }
        }
      }
    

    【讨论】:

    • 您的代码满足在列中添加图片的条件。感谢那。但排序似乎不太好。
    • 点击图片按钮排序没有发生。
    • 您是否正确启动 ImageButton.CommandName-property?它必须包含与 gridview 列关联的数据源的列名。
    • @vladimir77 让我们continue this discussion in chat
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多