允许按列Gridview进行排序 [英] allow sorting by column gridview

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

问题描述

我正在编写从Data Acess Layer获取数据并在GridView中显示数据的项目。
问题是允许按列排序。当我点击columnt以下的头发生以下错误:



异常详细信息:System.Web.HttpException:GridView的 'GridView1' 触发事件排序,这还没有被处理。



这里的.cs代码:

  public partial class默认值: System.Web.UI.Page 
{
EmployeesTableAdapter eta = new EmployeesTableAdapter();

保护无效的Page_Load(对象发件人,EventArgs的)
{
如果(!的IsPostBack)
{
GridView1.DataSource = eta.GetData() ;
GridView1.DataBind();
}
}
}

这里.aspx代码只有gridview):

 < asp:GridView ID =GridView1runat =serverAllowPaging =True
AllowSorting =TrueCellPadding =4ForeColor =#333333GridLines =None
AutoGenerateColumns =False>
< AlternatingRowStyle BackColor =White/>

<列>
< asp:TemplateField HeaderText =Select>
< ItemTemplate>
< asp:CheckBox ID =CheckBox1runat =server>
< / asp:CheckBox>
< / ItemTemplate>
< / asp:TemplateField>

< asp:BoundField DataField =FirstNameHeaderText =FirstNameSortExpression =FirstName/>
< asp:BoundField DataField =LastNameHeaderText =LastNameSortExpression =LastName/>
< asp:BoundField DataField =CountryHeaderText =CountrySortExpression =Country/>

< asp:TemplateField HeaderText =查看>
< ItemTemplate>
< asp:ImageButton ID =ImageButton1runat =serverImageUrl =images / view.png/>
< / ItemTemplate>
< / asp:TemplateField>

< asp:TemplateField HeaderText =编辑>
< ItemTemplate>
< asp:ImageButton ID =ImageButton2runat =serverImageUrl =images / edit.png/>
< / ItemTemplate>
< / asp:TemplateField>

< /列>
< EditRowStyle BackColor =#2461BF/>
< FooterStyle BackColor =#507CD1Font-Bold =TrueForeColor =White/>
< HeaderStyle BackColor =#507CD1Font-Bold =TrueForeColor =White/>
< RowStyle BackColor =#EFF3FB/>
< SelectedRowStyle BackColor =#D1DDF1Font-Bold =TrueForeColor =#333333/>
< SortedAscendingCellStyle BackColor =#F5F7FB/>
< SortedAscendingHeaderStyle BackColor =#6D95E1/>
< SortedDescendingCellStyle BackColor =#E9EBEF/>
< SortedDescendingHeaderStyle BackColor =#4870BE/>
< / asp:GridView>

Someony是否知道如何允许按列排序?

解决方案

GridView不自行排序。您需要为GridView排序事件添加一些代码并将其连接

首先,添加<$ c $的名称c> OnSorting 事件添加到 .aspx 页面上的 GridView

 < asp:GridView ID =gridViewOnSorting =gridView_Sortingrunat =server/> 

然后,在代码隐藏中添加该事件。 这个例子也可以处理改变排序方向 - 一旦用户排序,他们可能想要反转

 私有字符串ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch(sortDirection)
{
case SortDirection.Ascending:
newSortDirection =ASC;
休息;

case SortDirection.Descending:
newSortDirection =DESC;
休息;
}

return newSortDirection;


protected void gridView_Sorting(object sender,GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;

if(dataTable!= null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression ++ ConvertSortDirectionToSql(e.SortDirection);

gridView.DataSource = dataView;
gridView.DataBind();
}
}


I am writing project which gets data from Data Acess Layer and show it in GridView. The problem is to allow sorting by column. When I click on head of columnt following occurs following error:

Exception Details: System.Web.HttpException: GridView 'GridView1' Trigger Events Sorting, which has not been processed.

Here the .cs code:

public partial class Default: System.Web.UI.Page
    {
        EmployeesTableAdapter eta = new EmployeesTableAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = eta.GetData();
                GridView1.DataBind();
            }
        }
    }

Here the .aspx code(only gridview):

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" 
        AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" />

<Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>                
                    <asp:CheckBox ID="CheckBox1" runat="server">
                </asp:CheckBox>
                </ItemTemplate>                
            </asp:TemplateField>  

            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
            <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

            <asp:TemplateField HeaderText="View">
                <ItemTemplate>                
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/view.png"/>
                </ItemTemplate>                
            </asp:TemplateField> 

            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>                
                    <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="images/edit.png"/>
                </ItemTemplate>
            </asp:TemplateField> 

        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
       </asp:GridView>

Does someony know how to allow sorting by columns?

解决方案

The GridView does not sort itself. You need to add some code for a GridView sorting event and wire it up.

First, you add the name of the OnSorting event to the GridView on the .aspx page:

<asp:GridView ID="gridView" OnSorting="gridView_Sorting" runat="server" />

Then, you add that event in the code-behind. This example also handles changing the sort direction -- once the user has sorted, they may want to reverse the sort direction, and you have to keep track of that.

   private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
       string newSortDirection = String.Empty;

   switch (sortDirection)
   {
      case SortDirection.Ascending:
             newSortDirection = "ASC";
             break;

      case SortDirection.Descending:
         newSortDirection = "DESC";
         break;
   }

   return newSortDirection;
}

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as DataTable;

   if (dataTable != null)
   {
      DataView dataView = new DataView(dataTable);
      dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}

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

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