asp.net 在网格视图中显示分页行,但隐藏页码

mftmpeh8  于 2023-02-06  发布在  .NET
关注(0)|答案(2)|浏览(143)

简单的问题。
我有一个ASP.netGridView(VS 2005),它有页码,但是当每页的行数小于最大值(〈10)时,Pager行就会消失,这使得我的网格视图看起来很难看,就像底部缺少一行一样。
我可以强制显示Pager行,但是我需要隐藏页码1,因为很明显我们在第一页!

<asp:GridView ID="gvFTUNSENT" runat="server" 
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="True" CssClass="gvCSS" Width="100%"
                    DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
                    GridLines="Vertical" AllowPaging="True" PageSize="10" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                    OnPreRender="GridView_PreRender" 
                    OnLoad="GridView_Load" 
                    OnRowDataBound="GridView_RowDataBound" >
                    <RowStyle Wrap="True" Height="48px" />
                    <Columns>
...blahblah
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="cssPager" BackColor="#6B696B" ForeColor="White" HorizontalAlign="Left" Height="100%" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <EmptyDataTemplate>
                        <asp:Label ID="lblNDF1" runat="server" Text="NO DATA FOUND" Font-Size="X-Large" Width="500px" style="text-align:center" />
                    </EmptyDataTemplate>
                    <EmptyDataRowStyle HorizontalAlign="Center" />
                </asp:GridView>

这里我强制显示寻呼机行...

protected void GridView_PreRender(object sender, EventArgs e)
{
    GridView gv = (GridView)sender;

    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)gv.BottomPagerRow;
    if (gvr != null)
        gvr.Visible = true;
}

但我不想看到第一页,所以我试了这个...

if (e.Row.RowType == DataControlRowType.Pager)
{
    //keep showing pager line even if there is only one row of data
    GridViewRow gvr = (GridViewRow)e.Row;
    if (gvr != null)
    {
        gvr.Visible = true;

        //...but hide page number if there is only one page
        if (gv.PageCount == 1)
        {
            gv.ShowFooter = true;
            gv.PagerSettings.Visible = false;
        }
    }
}

但它实际上隐藏了整个寻呼机行!不!我只想隐藏页码。
ShowFooter,看起来就像是一个封闭的盒子。但是它还是很难看。如果我能保持页面行显示并能隐藏其中的任何内容,我宁愿不使用它。也就是说,保持背景色不变。
还有别的主意吗?谢谢

cbeh67ev

cbeh67ev1#

试试这样

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
    if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
    { e.Row.Style.Add("color", "white"); }
 }

或者,如果你不想处理颜色,你可以试试这个(但我认为它不太健壮)

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.Pager && GridView1.PageCount==1 )
    {
        var a = e.Row.Controls;
        if (a.Count>0 && a[0] is TableCell)
        {
            var b = a[0].Controls[0].Controls[0] as TableRow;
            if (b != null)
            {
                //This is actually your page 1 text
                b.Cells[0].Text = "";
            }
         }
     }
}
u0njafvf

u0njafvf2#

尝试
e.行.控件.清除();
因斯特德
e.行.样式.添加(“颜色”,“白色”);
它工作得更好。

相关问题