2013-02-26 63 views
0

我使用GridView并尝试对其进行排序和分页。 我的DataBase上有20多行,但GridView只显示了10.
我设置了AllowPaging=true,但是注意到了。
此外我使用AllowSorting = trueOnSorting="GridView_Sorting"。但是,当我点击标题对该列的内容进行排序时,它不会输入在我的OnSorting="GridView_Sorting"上,它会直接跳转到GridView1_RowCommand为什么?
有时它只是给我这个错误:
Object reference not set to an instance of an object error排序和分页GridView问题

这里是我的代码:

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

我隐藏:

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

回答

0

添加的SortExpression属性为每个排序的列,通知其DataField。

+0

我更新了一些minuts以前的代码...我已经做到了,然后它转到我在OnSorting事件中设置的GridView_Sorting事件,它直接进入RowCommand事件。为什么? – Ghaleon 2013-02-26 14:34:15

+0

这是来自任何GridView单击事件的常见行为。由您来检查哪个命令触发了该事件(使用CommandName属性)。但它并不妨碍您对网格进行排序。通过RowCommand事件后,排序事件将被调用。 – lockdown87 2013-02-26 14:45:34

+0

我更新了我的问题,看我的代码隐藏......它仍然不排序= \ – Ghaleon 2013-02-26 14:51:24