网格视图页码隐藏在RowDataBound上

时间:2014-01-25 15:40:06

标签: asp.net gridview rowdatabound

我有一个网格视图并将allow page设置为true.it工作正常但是当我想隐藏某些列(例如ID)时,页码被隐藏了!

<asp:GridView ID="gvVisa" OnRowDataBound="gvVisa_RowDataBound" OnSelectedIndexChanged="gvVisa_SelectedIndexChanged" OnPageIndexChanging="gvVisa_PageIndexChanging" runat="server" AllowPaging="True" PageSize="5" AutoGenerateColumns="False" Font-Size="13px" Width="963px">
    <AlternatingRowStyle BackColor="#00CCFF" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" BorderStyle="None" />
    <Columns>
        <asp:BoundField HeaderText="شناسه" DataField="vsiId"></asp:BoundField>
        <asp:TemplateField HeaderText="تصویر ">
            <ItemStyle Width="150" />
            <ItemTemplate>
                <asp:Image ID="Image3" runat="server" ImageUrl='<%#Eval("visImageName") %>' Width="150" Height="100" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText=" نام کشور" ItemStyle-Width="600px" DataField="visCountry">
            <ItemStyle Width="600px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="وضعیت نمایش " DataField="isActive"></asp:BoundField>
        <asp:TemplateField HeaderText="وضعیت نمایش">
            <ItemStyle Width="100px" />
            <ItemTemplate>
                <asp:Image ID="Image1" Width="24px" Height="24px" ImageUrl="~/images/false.png" Visible='<%# !(bool)Eval("isActive") %>' runat="server" />
                <asp:Image ID="Image2" Width="24px" Height="24px" ImageUrl="~/images/true.png" Visible='<%# Eval("isActive") %>' runat="server" />

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="عملیات">
            <ItemStyle Width="100px" />
            <ItemTemplate>
                <asp:LinkButton Text="تغییر وضعیت" Width="100px" CssClass="btns" ID="lnkSelect" runat="server" CommandName="Select" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#333333" ForeColor="White" />
    <RowStyle BackColor="White" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>  

绑定到DB:

protected void Page_Load(object sender, EventArgs e)
{
    gvVisa.DataSource = from q in new Data.TouristDataContext().Visas select q;
    gvVisa.DataBind();
}  

页面索引更改事件:

protected void gvVisa_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvVisa.PageIndex = e.NewPageIndex;
}  

和Row Data Bound事件使某些列不可见:

protected void gvVisa_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false;
}  

如何解决此问题?

enter image description here

1 个答案:

答案 0 :(得分:1)

在设置可见性之前,请检查它是否为DataRow:

编辑:经过一些修改,您可以解决编辑问题中提到的问题。

protected void gvVisa_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
    {
        // I would rather set display:hidden for ID
        e.Row.Cells[0].Style.Add("display", "none");
        //e.Row.Cells[0].Visible = false;
    }            
}  

if (e.Row.RowType == DataControlRowType.DataRowDataControlRowType.Pager)更改为if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)

Here's我用过的测试项目。我测试的输出看起来像这样:

enter image description here