0

刚看了几篇文章,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();
  }
4

2 回答 2

1

更新 2

现在我希望找到了您的寻呼机未显示的原因。您正在使第一列不可见,即使对于 pager-row in 也是如此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为true吗?

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

GridView.AllowPaging 属性

true如果启用了寻呼功能;否则,false。默认值为false.

您还应该仅在启用 viwstateGridView时才进行数据绑定(默认)。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");
    }
}
于 2013-02-26T19:11:23.693 回答
0

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

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

你可能在这里遇到同样的问题

更新

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

于 2013-02-26T19:15:36.537 回答