简体   繁体   中英

Asp.Net C# GridView first cell in third column is empty but data exists

Hi and thanks in advance.

I have a gridview that has four columns. The fourth column is hidden or displayed based on a set of criteria which is working correctly. But when the table displays, the very first cell in that last column is not showing any text, even though on databound it clearly shows that it has the text. So the gridview winds up showing data like this:

   Name         Address           Zip Code
 X H. Smith     123 Raton Ave. 
 X A. Rally     345 6th St        98453
 X B. Holcomb   876 Harrison Blvd 56321

The OnRowDataBound looks like this:

protected void gvAddresses_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //This test evaluation string showed the correct data for every row
        //string myvalue = DataBinder.Eval(e.Row.DataItem, "ZipCode").ToString();
        if (user.State == (int)States.California) 
            gvAddresses.Columns[3].Visible = true;

    }
}

There doesn't seem to be any problem with the binding and when I debug through the gridview each row is processed as I would expect. Here is the gridview:

 <asp:GridView runat="server" ID="gvAddresses" CssClass="TableFormat gvMargin"       Width="100%" AutoGenerateColumns="false" 
      OnRowCommand="gvAddresses_OnRowCommand" OnRowDeleting="gvAddresses_OnRowDeleting" OnRowDataBound="gvAddresses_OnRowDataBound">
      <EmptyDataTemplate>
           <div style="text-align: center;">No addresses available.</div>
      </EmptyDataTemplate>
      <Columns>
      <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
           <ItemTemplate>
                 <asp:LinkButton ID="lnkRemoveAddress" runat="server" OnClientClick="if(!confirm('Are you sure you want to delete this record?')) return ;"  style="color: maroon; font-weight: bold;" Text="X" CommandName="Delete" CommandArgument='<%# Eval("AddressCode") %>'></asp:LinkButton>
           </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:BoundField DataField="Address" HeaderText="Address" />
      <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" Visible="False"/>
      </Columns>
  </asp:GridView>

How else can I discover what is happening to this first cell?

First problem is this: you are checking every individual row and setting the entire column to visible or not over and again. You should check for State before you databind, since it is the user object I would suggest doing it on preinit.

I suggest you remove the visible=false from your markup. When you set visible to false then the control/object isn't rendered into the HTML. Your problem will most likely be fixed if you remove the visibility attribute from the markup and assign that value at the proper time in codebehind.

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        if (!postback)
        {
            gvAddresses.Columns[3].Visible = User.State == (int)States.California;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM