Results 1 to 10 of 10

Thread: [RESOLVED] Gridview and hidden columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Resolved [RESOLVED] Gridview and hidden columns

    I am populating a gridview from a database

    Code:
     sql = "SELECT* FROM tblFaultlog ORDER BY sys_date ASC"
                GridView1.DataSource = sqlquery()
                GridView1.DataBind()
    sqlquery return a datatable with the information I need, I do populate the datatable with some data but in the gridview I make one column.visable = false

    Why is it that if it is not visable I can't call on that column progromatically?

    it's as if the gridview doesn't retain the data for that column.

    Thanks in advance.

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Gridview and hidden columns

    from what i remember once you make the column visible property to false the column is not been rendered and therefor you can get its data, try to get the data you want first and then set the column to hidden.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Gridview and hidden columns

    Pretty much exactly as motil as described.

    If the column isn't set to visible, then it isn't rendered to the page, so there is no way for you to call into it programmatically, as it never goes into the ViewState for the page. If you keep a reference to the DataTable around though, you can still call into the columns of the DataTable if you need to.

    Gary

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Re: Gridview and hidden columns

    My datatable is populated via this function

    Code:
      Function sqlquery() As DataTable
            Dim da As New OleDb.OleDbDataAdapter(sql, dbconn)
            dbconn.Open()
            da.Fill(ds, "fault")
            dbconn.Close()
            Maxrows = ds.Tables("fault").Rows.Count
    
            dt.Columns.Add(New DataColumn("Location", GetType(String)))
            dt.Columns.Add(New DataColumn("Staff_Member", GetType(String)))
            dt.Columns.Add(New DataColumn("Problem_Description", GetType(String)))
            dt.Columns.Add(New DataColumn("SYS_Date", GetType(String)))
            dt.Columns.Add(New DataColumn("ItemEffected", GetType(String)))
            dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
            dt.Columns.Add(New DataColumn("Work_done", GetType(String)))
    
            Dim dr As DataRow = dt.NewRow()
            inc = "0"
    
            For Each dr In ds.Tables("fault").Rows
                Dim imageurl As String
                If ds.Tables("fault").Rows(inc).Item(6) = "True" Then
                    imageurl = "~/images/closed.png"
                Else
                    imageurl = "~/images/open.png"
                End If
    
                dr = dt.NewRow()
    
                dr("Location") = ds.Tables("fault").Rows(inc).Item(1)
                dr("Staff_Member") = ds.Tables("fault").Rows(inc).Item(2).ToString
                dr("Problem_Description") = ds.Tables("fault").Rows(inc).Item(3)
                dr("SYS_Date") = ds.Tables("fault").Rows(inc).Item(4)
                dr("ItemEffected") = ds.Tables("fault").Rows(inc).Item(5)
                dr("ImageURL") = ResolveUrl(imageurl)
                dr("Work_Done") = ds.Tables("fault").Rows(inc).Item(7)
                dt.Rows.Add(dr)
    
                dr = dt.NewRow()
                inc = inc + 1
    
            Next
            ds.Clear()
    
            Return dt
        End Function
    I have placed the datatable with the rest of my open variables at the top
    Code:
    Dim dt As New DataTable()
    If I check dt.Rows.Count when within the sub that called sqlquery() then I get the correct data and everything is fine but when I try to access it from an event triggered by selecting a row in the gridview it shows as empty.

    Why does it clear itself after being used in the initial sub?

  5. #5
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Gridview and hidden columns

    You could add the hidden column field to the grid datakeys collection and access it from that collection. see here http://msdn.microsoft.com/en-us/libr....datakeys.aspx

    Why are you hiding the column?
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  6. #6
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Gridview and hidden columns

    again i have little knowledge with datasoruce/datatable/gridview since i'm not using those controls (rarely i use gridview) but in the line of code just about your "return dt" you got this line:
    Code:
    ds.Clear()
    i think this method of the datasource will clear all the data (datatable included) that stored inside the datasource and that the reason you have empty datatables, but you might want to wait for Gary and hear his opinion.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Re: Gridview and hidden columns

    I use the Dataset DS to populate my Datatable, I did try and remove the clear but still 0.

    Just read that having
    Code:
    Dim dt as New DataTable
    is why it resets? makes some sence but when I change it to
    Code:
    Dim dt as Datatable
    I get error "object reference not set, etc etc"

    Going to look at grid datakeys collection now
    I need to hide some of the information as its just not needed by the user but when they click on certain things it will use that data and the rest of the data on that selected row to calculate a result.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Re: Gridview and hidden columns

    Dont think I can use datakeyname as it is the primary key and some of my rows contain the same data.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Re: Gridview and hidden columns

    Ahh, if I don't clear the DS I can still use it. Changed out the Datatable for the Datasource.
    But I still get that dam error I so love "Object reference not set to an instance of an object."

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    156

    Resolved Re: Gridview and hidden columns

    Fixed it

    Code:
    Shared dt As DataTable
    Now I can access it in my other subs

    Thanks for your help all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width