|
-
Apr 8th, 2009, 07:23 AM
#1
Thread Starter
Lively Member
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...
-
Apr 8th, 2009, 09:38 AM
#2
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
-
Apr 8th, 2009, 02:26 PM
#3
Re: Populating GridView using Objects Collection instead of Dataset
Set the DataSource to a List<YourObjectType>.
-
Apr 9th, 2009, 03:19 AM
#4
Thread Starter
Lively Member
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
-
Apr 9th, 2009, 04:08 AM
#5
Fanatic Member
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.
-
Apr 9th, 2009, 04:14 AM
#6
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
-
Apr 9th, 2009, 05:35 AM
#7
Thread Starter
Lively Member
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
-
Apr 9th, 2009, 06:00 AM
#8
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
-
Apr 9th, 2009, 10:05 AM
#9
Thread Starter
Lively Member
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
-
Apr 9th, 2009, 10:28 AM
#10
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
-
Apr 11th, 2009, 07:32 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|