根据gridview双标题对列进行排序 [英] Sort columns based on gridview double header

查看:111
本文介绍了根据gridview双标题对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,我需要排序GridView的列。但问题在于,我需要在gridview列的下方放置一行,这些列会有升序和降序排列的图像。点击这些图片时,应该对所选列进行排序。



请指导我!!

让我知道,如果你有任何疑问/怀疑



谢谢!

解决方案

我的决定:

 < asp:GridView runat =serverID =GridViewTestDataSourceID =CustomersSourceAutoGenerateColumns = 假 > 
<列>
< asp:TemplateField>
< HeaderTemplate>
< asp:Panel runat =serverBorderWidth =1>
< asp:Label runat =serverText ='column {CustomerID}'>< / asp:Label>
< / asp:面板>
< asp:Panel runat =server>
< asp:ImageButton runat =serverAlternateText =ascCommandName =CustomerIDCommandArgument =<%#CommandArgumentAsc%> OnClick =ImageButton_Click/>
< asp:ImageButton runat =serverAlternateText =descCommandName =CustomerIDCommandArgument =<%#CommandArgumentDesc%> OnClick =ImageButton_Click/>
< / asp:面板>
< / HeaderTemplate>
< ItemTemplate>
<%#Eval(CustomerID)%>
< / ItemTemplate>
< / asp:TemplateField>

< asp:TemplateField>
< HeaderTemplate>
< asp:Panel runat =serverBorderWidth =1>
< asp:Label runat =serverText ='column {CompanyName}'>< / asp:Label>
< / asp:面板>
< asp:Panel runat =server>
< asp:ImageButton runat =serverAlternateText =ascCommandName =CompanyNameCommandArgument =<%#CommandArgumentAsc%> OnClick =ImageButton_Click/>
< asp:ImageButton runat =serverAlternateText =descCommandName =CompanyNameCommandArgument =<%#CommandArgumentDesc%> OnClick =ImageButton_Click/>
< / asp:面板>
< / HeaderTemplate>
< ItemTemplate>
<%#Eval(CompanyName)%>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

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

后面的代码:

 受保护的常量字符串CommandArgumentAsc =asc; 
受保护的const字符串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);
}
}
}


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!

解决方案

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

这篇关于根据gridview双标题对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆