C# 排序和分页GridView问题

C# 排序和分页GridView问题,c#,asp.net,sorting,gridview,paging,C#,Asp.net,Sorting,Gridview,Paging,我正在使用GridView并尝试对其进行排序和分页。 我的数据库中有20多行,但GridView仅显示10行。 我设置了allowpage=true但没有发生任何事情。 我还使用了AllowSorting=true和OnSorting=“GridView\u排序”。但是,当我单击标题对该列的内容进行排序时,它不会进入我的OnSorting=“GridView\u Sorting”命令,而是直接进入GridView1\u行命令why? 有时它只会给我这个错误: 对象引用未设置为对象错误的实例 这是

我正在使用
GridView
并尝试对其进行排序和分页。 我的数据库中有20多行,但GridView仅显示10行。
我设置了
allowpage=true
但没有发生任何事情。
我还使用了
AllowSorting=true
OnSorting=“GridView\u排序”
。但是,当我单击标题对该列的内容进行排序时,它不会进入我的
OnSorting=“GridView\u Sorting”
命令,而是直接进入
GridView1\u行命令
why?
有时它只会给我这个错误:
对象引用未设置为对象错误的实例

这是我的密码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif;" 

    CssClass="gridview" OnSorting="GridView_Sorting"
    AllowSorting ="True" AllowPaging="True" BackColor="#CCCCCC" 
    BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
    CellPadding="1"
    CellSpacing="5"
    HeaderStyle-HorizontalAlign="Center"
    OnRowDataBound="GridView1_RowDataBound"
    ForeColor = "Black" RowStyle-CssClass="gridview" 
    OnRowCommand="GridView1_RowCommand">            
    <AlternatingRowStyle BackColor="#CCCCCC" />
       <columns>
          <asp:BoundField HeaderText="ID" DataField="id" SortExpression="id"  />
          <asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" ItemStyle-HorizontalAlign="Center" SortExpression="prioridade" SortExpression="prioridade" />
          <asp:BoundField  HeaderText="SITUAÇÃO"  DataField="situacao" ItemStyle-HorizontalAlign="Center" >
          <ItemStyle HorizontalAlign="Center" />
          </asp:BoundField>
          <asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
          <HeaderStyle Width="65px" />
          <ItemStyle HorizontalAlign="Center" />
          </asp:BoundField>
          <asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" SortExpression="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"ItemStyleHorizontalAlign="Center" >
          <HeaderStyle Width="60px" />
          <ItemStyle HorizontalAlign="Center" />
          </asp:BoundField>
          <asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
          ItemStyle-HorizontalAlign="Center">
          <HeaderStyle Width="60px" />
          <ItemStyle HorizontalAlign="Center" />
          </asp:BoundField>
          <asp:BoundField HeaderText="PROJETO" DataField="projeto"  ItemStyle-HorizontalAlign="Center"></asp:BoundField>
          <asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" ItemStyle-HorizontalAlign="Center" />
          <asp:BoundField HeaderText="CLUBE" DataField="clube"  ItemStyle-HorizontalAlign="Center" />
          <asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
          <ItemTemplate>
          <asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid"  Font-Size="7pt" Font-Names="Verdana, Arial"  OnClick="Btn_Visualizar_Click"CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />                            
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
          <ItemTemplate>
          <asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
          </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderStyle-Width="66px" HeaderText="FEEDBACK">
       <ItemTemplate>
       <asp:Button ID="Btn_Feedback" runat="server" Text="ADICIONAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana,Arial"CommandName="feedback" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
       </ItemTemplate>
       </asp:TemplateField>
       </columns>

为每个可排序列添加SortExpression属性,通知其数据字段。

几分钟前我更新了代码。。。我已经这样做了,然后它转到我在
OnSorting
事件上设置的
GridView\u排序事件。。。它直接进入
RowCommand
事件。为什么?这是任何GridView点击事件的常见行为。由您检查哪个命令触发了事件(使用CommandName属性)。但是,这并不妨碍您对网格进行排序。在通过RowCommand事件后,将调用排序事件。我更新了我的问题,查看我的代码隐藏。。。它仍然没有排序=\我也不能分页,或滚动条它;有什么帮助吗?它至少到达排序事件了吗?我是说,它有没有碰到任何断点?
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');

            // If the sorting column is the same as the previous one,  
            // then change the sort order. 
            if (strSortExpression[0] == e.SortExpression) 
                { 
                if (strSortExpression[1] == "ASC") 
                { 
                    ViewState["SortExpression"] = e.SortExpression + " " + "DESC"; 
                } 
                else 
                { 
                    ViewState["SortExpression"] = e.SortExpression + " " + "ASC"; 
                } 
            } 
                // If sorting column is another column,   
                // then specify the sort order to "Ascending". 
            else 
            { 
                ViewState["SortExpression"] = e.SortExpression + " " + "ASC"; 
            } 
            // Rebind the GridView control to show sorted data. 
            GridView1.DataSource = ch.BuscaTodosChamados();
            GridView1.DataBind();
        }