分页GridView问题

时间:2013-02-26 19:05:45

标签: asp.net gridview pagination

我刚刚阅读了一些关于paging gridview的文章,但我无法做到......

我的完整gridview代码:

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

            CssClass="gridview" OnSorting="GridView_Sorting"
            DataKeyNames="id"
            AllowSorting ="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" AllowPaging="True" 
            OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">            
            <AlternatingRowStyle BackColor="#CCCCCC" />
                <columns>

                    <asp:BoundField HeaderText="ID" DataField="id"  />
                    <asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" 
                        ItemStyle-HorizontalAlign="Center">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <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" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px" SortExpression="dt_cadastro"
                    ItemStyle-HorizontalAlign="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">
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" 
                        ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="CLUBE" DataField="clube"  
                        ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>

                    <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>
                        <HeaderStyle Width="70px" />
                    </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>
                        <HeaderStyle Width="66px" />
                    </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>
                        <HeaderStyle Width="66px" />
                    </asp:TemplateField>

                </columns>
            <EditRowStyle ForeColor="Black" CssClass="GridViewEditRow" />
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
            <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            </asp:GridView>    

代码隐藏:

public partial class TodosChamados : System.Web.UI.Page
  {
    BDUsuarios usr = new BDUsuarios();
    BDFuncionalidades func = new BDFuncionalidades();
    BDChamados ch = new BDChamados();

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

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[1].Text == "ALTA")
            {
                e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
                e.Row.ControlStyle.Font.Bold = true;
            }
        }
        if (e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager)
        {
            e.Row.Cells[0].Visible = false;
        }
    }

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
    if (e.CommandName != "Sort")
       {
          if (e.CommandName == "visualizar")
                {
                 Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text;
                    Session["editar"] = null;
                }
                else if (e.CommandName == "editar")
                {
                    Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.
                    Session["editar"] = 1; // 1 - Editar   2 - Não Editar .
                }

                else if (e.CommandName == "feedback")
                {
                    Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.                
                    Response.Redirect("~/Adm/Feedback.aspx");
                }
                Response.Redirect("~/Adm/DetalhesChamado.aspx");
            }
        }

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
      GridView1.PageIndex = e.NewPageIndex;
      GridView1.DataSource = ch.BuscaTodosChamados();
      GridView1.DataBind();
  }

2 个答案:

答案 0 :(得分:1)

更新2

现在我希望找到你的寻呼机没有显示的原因。您正在使第一个列不可见,即使对于RowDataBound中的寻呼机行,但是默认情况下寻呼机位于第一个单元格中:

e.Row.Cells[0].Visible = false;

您必须检查正确的RowType

e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager;

甚至更好的aspx标记:

<asp:BoundField HeaderText="ID" DataField="id" Visible="False" />

一个愚蠢的问题,您是否将AllowPaging设为真?

<asp:GridView Id="GridView1" runat="server" AllowPaging="True" >
    <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
</asp:gridview>

GridView.AllowPaging Property

true如果启用了分页功能;否则,false。默认值为 false

启用viwstate时,您还应该仅对GridView if(!IsPostBack)进行数据绑定(默认)。

所以将您的数据绑定代码从Page_Load(我猜)包装到此检查中:

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         GridView1.DataSource = ch.BuscaTodosChamados();
         GridView1.DataBind();
    }
}

否则,在回发时重新加载数据时,不会触发事件并覆盖更改。

更新根据您的上一次修改:

我认为您的RowCommand是导致此问题的原因。它在PageIndexChanging事件之前被调用,并且当页面发生更改时也会被触发(如果我没记错的话)。所以,看看你的Response.Redirect,当没有if/else-if会抓住它时会发生这种情况。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName != "Sort")
    {
        if (e.CommandName == "visualizar")
        {
            // ..
        }
        else if (e.CommandName == "editar")
        {
           //..
        }

        else if (e.CommandName == "feedback")
        {
           //..
        }
        Response.Redirect("~/Adm/DetalhesChamado.aspx");
    }
}

答案 1 :(得分:0)

您必须在.aspx中指定OnPageIndexChanging的事件。它应该是:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        OnPageIndexChanging = "GridView1_PageIndexChanging" 
        AutoGenerateColumns="False" DataKeyNames="LastName,FirstName"
        EnablePersistedSelection="True" SelectedRowStyle-BackColor="Yellow" 
        DataSourceID="SqlDataSource1" AllowSorting="True">

您可能拥有相同的problem here

<强>更新

确保绑定到GridView的数据超过PageSize="5"以来的数据。如果行数小于5,则不会显示寻呼机。

相关问题