I am trying to hide a column in a gridview but need access to the data in these fields.

I have a gridview with columns that I want to hide but still need to access this data at the row level. What is the best way to achieve this? (Using Visual Studio 2013)

OK - I posted the question then posted the answer. I tried to find the answer and discovered the question was asked a
lot, and many had 30 or more posts on the question. Since I spent way to much time looking at forums/discussions and figuring it out, I thought that I should repost the question then answer it. Hopefully if someone stumbles on it it might save
them some time.
I tried setting column widths, setting columns to visible=false before a bind (and after a bind), CCS, TemplateFields etc etc. All had problems either in the display, paging or retrieving the data in the gridview events (RowCommand & RowDataBound).
I am not saying the others won't work but this is the way I got it to work. As with most coding issues once you have the answer it looks pretty simple.  
First of all forget about trying to hide columns - Use DataKeyNames in the GridView. 
Here is what I am trying to do - Based on the data in the "hidden columns" in the row, I want to manipulate how the Grid will appear. (Buttons will or will not appear)
SQL:
Select [FruitID], [StuffDesc], [ActionAFlag], [ActionBFlag] from [Friut] ORDER BY [StuffDesc]
ASPX:
      <asp:gridview id="Gridview1"  AllowPaging="True" runat="server" 
                    autogeneratecolumns="False"
                    onrowdatabound="GridView1_RowDataBound"  
                    onrowcommand="GridView1_RowCommand"
                    Datakeynames="FruitID, ActionAFlag, ActionBFlag">
          <Columns>
              <asp:BoundField DataField="StuffDesc"  Headertext="Made up Stuff" />
              <asp:ButtonField ButtonType="Button" CommandName="FireA" Text="Action A" />
              <asp:ButtonField ButtonType="Button" CommandName="FireB" Text="Action B" />
          </Columns>
      </asp:gridview>
ASPX.VB
RowDataBound fires before each row is rendered to the Gridview. The data in ActionAFlag and ActionBFlag will determine which buttons show. If the data in field ActionAFlag is 1 then "Action A" button shows; if field ActionBFlag
is 1 then "Action B" shows. First it checks to see that you are dealing with a DataRow, (not Headers or Footers). Note variable "rowcounter" [I could not figure out how to get the row number from GridviewrowEventsArgs so I fudged it by
using a global variable and setting it to 0. Each time the page is rendered the Rowcount is automatically set to 0]
    Protected Sub GridView1_RowDataBound(ByVal Sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim AShow As String = Gridview1.DataKeys(rowcounter).Values("ActionAFlag").ToString
            Dim BShow As String = Gridview1.DataKeys(rowcounter).Values("ActionBFlag").ToString
            If  AShow <> "1" Then
                e.Row.Cells(1).Text = " "
                ' -- Blank Action A Button ---
            End If
            If BShow <> "1" Then
                e.Row.Cells(2).Text = " "
                ' -- Blank Action B Button ---
            End If
            rowcounter = rowcounter + 1
        End If
RowCommand fires on page changes or if a button on a row is clicked. I want to capture the FruitID and execute the appropriate button action. Here I can get the Row Index
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "FireA" Or e.CommandName = "FireB" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim FruitID As String = Gridview1.DataKeys(index).Values("FruitID").ToString
            If e.CommandName = "FireA" Then
                MsgBox(" In FireA / FruitID = " + FruitID)
            Else
                MsgBox(" In FireB /FruitID = " + FruitID)
            End If
        End If
    End Sub

Similar Messages

Maybe you are looking for

  • How to I point to my itunes library with my new computer?

    I have recently got a new computer and so I've reinstalled itunes.  I backed up all my music from my old machine and have copied it to my new machine.  How do I tell my new machine where that folder is?  All the music is in folders under C:\iTunes Mu

  • Flash Builder 4.5 Java Heap Space Error

    Hello everyone, I've downloaded Flash Builder 4.5 trial a few days ago. Let me first tell that I like the new features a lot and they help to speed up the development process noticably. I thank Adobe for that very much. However I did not notice any p

  • Why is The iTunes Library Extras.itdb file locked?

    After my walk this morning I tried to connect up to iTunes to chargemy ipod and update my tunes and got an error message: "The iTunes Library.itdb file is locked, on a locked disc, or you do not have write permission for this file." WHY WHY WHY ???

  • Saving a Mac Word file by printing to PDF not possible anymore

    I have a  old version of Office v.X for Mac that will no longer allow me to save files by printing them as  save to PDF. I think the problem occured when I switched to Mac OS X 10.6.8. Does anyone know what the problem maybe and how I can fix it?

  • Is there any documentation on TSAutoMgr.exe?

    I noticed a while back that the TSAutoMgr.exe server is executed whenever you create a TestStand engine. I peeked at the tlb for the server and it looks like you wrote a remotable version of the GIT. Cool. Unfortunately there's no way to discover the