Results 1 to 11 of 11

Thread: Populating GridView using Objects Collection instead of Dataset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    Populating GridView using Objects Collection instead of Dataset

    Hi All

    Is there an easy way to
    Populat GridView using Objects Collection instead of Dataset

    i know this is how it works with dataset

    gridview1.DataSource = Dataset.Tables(TABLE1)

    but i want to

    For each ObjCustomer in ObCustomers

    'Add record to grid??

    Next

    Thanks in advance...

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

    Re: Populating GridView using Objects Collection instead of Dataset

    Hey,

    What is the Type of the collection that you are speaking of?

    You should be able to assign the collection directly to the DataSource property of the GridView. You shouldn't need to first loop through all the records in the collection.

    Can you show what you are trying to do?

    Gary

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Populating GridView using Objects Collection instead of Dataset

    Set the DataSource to a List<YourObjectType>.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    Re: Populating GridView using Objects Collection instead of Dataset

    Awsome that worked. Thankyou very much

    passing in the Collection Class to the datasource property shows all the data :-)

    GridView1.PageIndex = 0
    GridView1.DataSource = objCustomers

    GridView1.DataBind()


    Can I hide columns which i dont want to display??

    Cheers

  5. #5
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Populating GridView using Objects Collection instead of Dataset

    Would you mind showing the code that creates objCustomers? I keep hearing about people using objects instead of datasets but don't really understand what they mean - or the advantages of using objects instead of datasets.

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

    Re: Populating GridView using Objects Collection instead of Dataset

    Hey,

    You can use an ObjectDataSource to display the contents of custom created Business Object, which is essentially a class with lots of properties, depending on what the Object represents, and that has methods such as GetCustomer, UpdateCustomer, DeleteCustomer, NewCustomer, which you can bind to the ObjectDataSource methods, for controlling the display and modification of the data contained within the Object.

    I use the above approach, and this is because I feel that it gives you more control over the Objects, but there are arguments for and against and it has been the topic of debate for a long time now.

    As for your question ragstta, it probably makes sense to only pull back the information for the database that you actually want. In which case you might want to create overloaded get methods, i.e. GetAllCustomers(), GetRecentCustomers(), GetOldCustomers() (bad examples I know, but you get the idea), and use these methods in the ObjectDataSource to only display the information you want.

    Gary

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    Re: Populating GridView using Objects Collection instead of Dataset

    Cool yep those all are good ideas. I've found this

    Code:
        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
            Dim i As Integer
            Dim strColumnsInterestedIn As String
            
            strColumnsInterestedIn = "[1]"
            
            For i = 0 To (e.Row.Cells.Count - 1)
                
                'Hide Columns we are not interested in
                If Not strColumnsInterestedIn.Contains("[" & i.ToString & "]") Then
                    e.Row.Cells(i).Visible = False
    
                End If
                
                'Rename Columns names
                If e.Row.Cells(i).Text = "DBName" Then
                    e.Row.Cells(i).Text = "Database name"
                End If
            Next i
    
        End Sub
    It lets me remove the columns i dont want. Its not pretty but does the job.
    Im still playing with concepts so all ur advice is really useful cheers

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

    Re: Populating GridView using Objects Collection instead of Dataset

    Hey,

    I guess it comes down to how many rows we are talking about.

    If there are lots, there bringing them back from the database, then looping through them only to hide them seems a little unintuitive, and may result in the page taking longer to load than it normally would. Something to keep in mind as the application grows.

    Gary

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    Re: Populating GridView using Objects Collection instead of Dataset

    Yep defo, i might just populate my dataset from my object.
    main problem is we are still using ado the old ado
    so our business layer returns ado recordsets and not datasets :-s

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

    Re: Populating GridView using Objects Collection instead of Dataset

    I think the general consensus would be to upgrade to ADO.Net when you get a chance.

    Gary

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Populating GridView using Objects Collection instead of Dataset

    Yes, definitely move over.

    Also, if you only want certain fields visible, then consider going down the TemplateField route rather than setting a few columns to invisible so that you have control over what the columns look like, what is displayed and what isn't displayed.

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