Hiding columns in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    Hiding columns in GridView

    Hi all,

    Hopefully this one is rather simple. I just haven't been able to figure it out.

    On my GridView I have some hidden key columns which I've hidden by using tha attribute Visible="false" in the TemplateField declaration. This column is later used as an UpdateParameter field.

    Now if I leave the field as Visible="true", everything works fine and down in my C# code, I can easily access the UpdateParameter field with e.Command.Param eters[0].Value. However, when I set Visible to false, the call to e.Command.Param eters[0].Value always returns null.

    So how can I hide this column from the users, but still use it as a parameter field?

    Thanks for any assistance.

    Robert
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by RobertTheProgra mmer
    Hi all,

    Hopefully this one is rather simple. I just haven't been able to figure it out.

    On my GridView I have some hidden key columns which I've hidden by using tha attribute Visible="false" in the TemplateField declaration. This column is later used as an UpdateParameter field.

    Now if I leave the field as Visible="true", everything works fine and down in my C# code, I can easily access the UpdateParameter field with e.Command.Param eters[0].Value. However, when I set Visible to false, the call to e.Command.Param eters[0].Value always returns null.

    So how can I hide this column from the users, but still use it as a parameter field?

    Thanks for any assistance.

    Robert
    I ran into the same problem once.
    It's because when you user the .NET Visible=False the field is never rendered in browser...there fore when it comes back to the server and you try to access it there isn't anything there.

    What I've done is used CSS to hide the column.
    I set the CssClass of the item to be the Css class "noShow"
    eg:
    [code=asp]
    <asp:BoundFie ld HeaderText="the Index" DataField="theI ndex">
    <ItemStyle CssClass="noSho w" Width="0px" />
    <HeaderStyle CssClass="noSho w" Width="0px" />
    </asp:BoundField>
    [/code]

    Where my 'noShow" Css class was
    [code=css]
    .noShow
    {
    display:none;
    }
    [/code]

    -Frinny

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      Works beautifully. Thanks!

      Robert

      Comment

      • RobertTheProgrammer
        New Member
        • Aug 2007
        • 58

        #4
        One anomaly I noticed with this idea… it doesn’t seem to hide the FooterTemplate columns. I’ll continue to try to figure it out, but if you have any clues, please let me know.

        Robert

        Comment

        • RobertTheProgrammer
          New Member
          • Aug 2007
          • 58

          #5
          DOH! I feel like a doofus.

          <FooterStyle CssClass="noSho w" Width="0px" />

          Solved my problem.

          Robert

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by RobertTheProgra mmer
            DOH! I feel like a doofus.

            <FooterStyle CssClass="noSho w" Width="0px" />

            Solved my problem.

            Robert
            Don't feel too bad, it took me a while to even think about using CSS to format the look of my GridView.

            -Frinny

            Comment

            • RobertTheProgrammer
              New Member
              • Aug 2007
              • 58

              #7
              Okay, here's one that's similar, but I haven't been able to solve.

              I want to dynamically hide the columns. In other words, during the page_load I get information about the current user, so later (on Page_Load, OnPageRender, OnRowDataBound-- I'm not sure) I want to selectively hide some columns from the user based upon information I'd earlier gathered during the page_load. I can't do this directly in the ASP code, so I'm assuming I'll do it down in the C# code by setting the Itemstyle, Headerstyle and Footerstyle of the TemplateField.

              How does one do this? I've been trying for the last two hours to figure it out, but I'm at a loss.

              Robert

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                You can either do a post process after the data has been loaded into the columns or you can do something in the RowDataBound event.

                As for the original problem, seems like you can access the row directly from like mygridview.Rows[<whatever>] and get the value of the hidden column. In the click events (and others) you are usualy given the row index value so you can find the correct row and get the value from your hidden column through that method.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by RobertTheProgra mmer
                  Okay, here's one that's similar, but I haven't been able to solve.

                  I want to dynamically hide the columns. In other words, during the page_load I get information about the current user, so later (on Page_Load, OnPageRender, OnRowDataBound-- I'm not sure) I want to selectively hide some columns from the user based upon information I'd earlier gathered during the page_load. I can't do this directly in the ASP code, so I'm assuming I'll do it down in the C# code by setting the Itemstyle, Headerstyle and Footerstyle of the TemplateField.

                  How does one do this? I've been trying for the last two hours to figure it out, but I'm at a loss.

                  Robert
                  It's probably easiest in your RowDataBound event.

                  [code=vbnet]
                  Protected Sub MyGridView_RowD ataBound(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles GV_MyGridView.R owDataBound

                  If myUser.IsAllowe dToViewMyFirstC olumn() = False Then
                  e.Row.Cells(0). CssClass = "noShow"
                  End If
                  End Sub
                  [/code]

                  [code=cpp]
                  Protected Void MyGridView_RowD ataBound(Object sender,System.W eb.UI.WebContro ls.GridViewRowE ventArgs e)
                  {
                  If (myUser.IsAllow edToViewMyFirst Column() == False)
                  { e.Row.Cells[0].CssClass = "noShow";}
                  }
                  [/code]

                  -Frinny

                  Comment

                  • RobertTheProgrammer
                    New Member
                    • Aug 2007
                    • 58

                    #10
                    Okay, you learn something new every day. I had assumed-- falsely apparently-- that the OnRowBound event would only affect the specific row on which it was called. I needed to hide the header and footer as well and since these are not necessarily bound (my assumption-- maybe they are), it would not fire the OnRowBound event. But it works as you describe.

                    Many thanks for the responses.

                    Robert

                    Comment

                    Working...