代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_C#

GridView固定行以后的翻页有关问题

发布时间:2019-12-09浏览(2199)

    GridView固定行以后的翻页问题
    GridView进行翻页没有问题,但是因为需要当页面数据不满18时补空行到18行,从网上找了代码过来是可以了,
    但是翻页发生问题了,比如有2页,点前一页的时候,显示的还是刚才那页的内容,而且补的空白行也没有了,
    再点一次才能正常回到第一页。代码如下,帮忙看看啊,谢谢谢谢!
    HTML code
    
    <asp:GridView ID="gvProgram" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="ID" 
        DataSourceID="FileDataSource" OnDataBound="gvProgram_DataBind" 
        OnPageIndexChanging="gvProgram_PageIndexChanging" OnRowCommand="gvProgram_RowCommand" 
        Height="498px" Width="531px" PageSize="18" OnRowDataBound="gvProgram_RowDataBound"  
        >
        <PagerTemplate>
            <table width="100%">
                <tr>
                    <td>
                        <asp:ImageButton ID="imageFirst" CommandName="Page"  CommandArgument="First"  runat="server"   />
                        <asp:ImageButton ID="imagePrevious" CommandName="Page" CommandArgument="Prev"  runat="server"/>&nbsp;
                        <asp:ImageButton ID="imageNext" CommandName="Page" CommandArgument="Next" runat="server" />
                        <asp:ImageButton ID="imageLast" CommandName="Page" CommandArgument="Last" runat="server"/>
               </td>
                </tr>
            </table>      
        </PagerTemplate>
        <Columns>
            <asp:BoundField DataField="FileName" HeaderText="标题" >
            </asp:BoundField>
            <asp:BoundField DataField="FileSize" HeaderText="文件大小">
            </asp:BoundField>
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="FileDataSource" runat="server" 
        SelectMethod="GetListByFileTypeAndHost" TypeName="BLLFile">
        <SelectParameters>
            <asp:Parameter DefaultValue="02" Name="fileType" Type="String" />
            <asp:Parameter DefaultValue="00" Name="host" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>
    
    


    C# code
    
          protected void gvProgram_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // 计算自动填充的行数
                    numCount++;
                }
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    // 计算完毕,在此添加缺少的行
                    int toLeft = this.gvProgram.PageSize - numCount;
                    int numCols = this.gvProgram.Rows[0].Cells.Count;
                    DataControlRowState rowState = (this.gvProgram.Rows[numCount - 1].RowState);
    
                    for (int i = 0; i < toLeft; i++)
                    {
                        // 正确显示交叉行样式
                        rowState = (i % 2 != 0) ? DataControlRowState.Alternate : DataControlRowState.Normal;
    
                        GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, rowState);
                        for (int j = 0; j < numCols; j++)
                        {
                            TableCell cell = new TableCell();
                            cell.Text = "&nbsp;";
                            row.Cells.Add(cell);
                        }
                        this.gvProgram.Controls[0].Controls.AddAt(numCount + 1 + i, row);
                    }
                }
            }
    
      protected void gvProgram_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                int newPageIndex = 0;
               // 当点击first, last, previous and next按钮时处理
                    newPageIndex = e.NewPageIndex;
                //  对PageIndex溢出处理
                newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                newPageIndex = newPageIndex >= this.gvProgram.PageCount ? this.gvProgram.PageCount - 1 : newPageIndex;
                // 更新PageIndex
                this.gvProgram.PageIndex = newPageIndex;
            }