Hiding a column

Discussion in 'ASP .Net Datagrid Control' started by dev648237923, Nov 17, 2006.

  1. dev648237923

    dev648237923 Guest

    I do my updating in the code-behind (long story). I need an ID field in my
    DtatGrid to pass to my Stored Proceedure but I do not want that column to
    show up on the screen. If I set the GridView column to Visible="false" then
    in the code behind when I need to retrieve the ID the below gives an error:
    int MyID = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);

    So I came up with what I think works well for hiding the column but still
    leaving it visible to the code behind:
    In the asp:DataBound tag I do: HeaderStyle-CssClass="hidetd"
    ItemStyle-CssClass="hidetd"

    Then in my stylesheet I have:
    ..hidetd { display: none; }

    This causes the column not to show on the screen but to still be there so I
    can get items from it with GridView1.Rows[e.RowIndex].Cells[1].Text.

    I would appretiate if there is a better way or if this way has any unforseen
    flaws to hear your comments.

    Thank you!
     
    dev648237923, Nov 17, 2006
    #1
    1. Advertising

  2. This is the correct way.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "dev648237923" <> wrote in message
    news:%...
    >
    > I do my updating in the code-behind (long story). I need an ID field in my
    > DtatGrid to pass to my Stored Proceedure but I do not want that column to
    > show up on the screen. If I set the GridView column to Visible="false"
    > then in the code behind when I need to retrieve the ID the below gives an
    > error:
    > int MyID = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
    >
    > So I came up with what I think works well for hiding the column but still
    > leaving it visible to the code behind:
    > In the asp:DataBound tag I do: HeaderStyle-CssClass="hidetd"
    > ItemStyle-CssClass="hidetd"
    >
    > Then in my stylesheet I have:
    > .hidetd { display: none; }
    >
    > This causes the column not to show on the screen but to still be there so
    > I can get items from it with GridView1.Rows[e.RowIndex].Cells[1].Text.
    >
    > I would appretiate if there is a better way or if this way has any
    > unforseen flaws to hear your comments.
    >
    > Thank you!
    >
    >
     
    Eliyahu Goldin, Nov 19, 2006
    #2
    1. Advertising

  3. Hi dev648237923,

    As you said that you're currently manually performing the update in code
    behind, then how do you retrieve the data from GridView Row(being edit),
    manually get the Cell's InnerText or Find the inner control and read value?

    If you're already used the "Findcontrol and read value from inner control"
    approah, you can also consider using this way to store and retrieve the ID
    field in GridView. To this, you can follow the below steps:

    1. Add an html input hidden control(mark as runat="server") into the
    tempatefield with other control and bind the ID data to this html hidden
    field's "value" property. e.g.

    =====================
    <asp:TemplateField HeaderText="Description" SortExpression="Description">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%#
    Bind("Description") %>'></asp:TextBox>
    <input type="hidden" runat="server" id="hdID"
    value='<%# Bind("CategoryID") %>' />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("Description") %>'></asp:Label>
    <input type="hidden" runat="server" id="hdID"
    value='<%# Bind("CategoryID") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    ========================

    2. In code behind event, you can Find this hidden field and retrieve ID
    value from it for updating.

    ===============
    protected void GridView1_RowUpdating(object sender,
    GridViewUpdateEventArgs e)
    {
    GridViewRow row = GridView1.Rows[e.RowIndex];

    HtmlInputHidden hidden = row.FindControl("hdID") as HtmlInputHidden;

    Response.Write("<br/>ID: " + hidden.Value);

    }
    ================


    Also, there are other means to do so. for example, you can also add the ID
    value into an existing control's Attributes collection in GridView's
    "RowDataBound" event and retrieve it later. e.g.

    =====================
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
    e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Attributes["ID"] = xxxx;
    }
    }
    ==========================

    Hope this helps. If you have anything unclear on this, please feel free to
    post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Nov 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle Stone
    Replies:
    0
    Views:
    418
    Michelle Stone
    Oct 15, 2003
  2. beccak
    Replies:
    4
    Views:
    4,969
    beccak
    Jun 9, 2004
  3. David Freeman

    DataGrid - Hiding a Column - Not Working

    David Freeman, Oct 22, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    508
  4. BobRoyAce
    Replies:
    2
    Views:
    4,525
    BobRoyAce
    Jan 20, 2005
  5. Ste
    Replies:
    41
    Views:
    811
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page