Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

How to Bind List<> to GridView with Custom Objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Wood - 20 Jan 2008 03:18 GMT
I see that I can create a List<string> list and bind it to a GridView
control (by setting the DataSource property to the list and calling the
DataBind method).

What's the trick to doing the same thing with my own classes instead of a
simple string? Is there an interface or something I can implement so that
the GridView control can detect the properties of my object and display them
?

Thanks.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Nick Bennett - 20 Jan 2008 03:27 GMT
You can just set the DataSource to be a List<MyObject> and DataBind. You
won't get any help with setting up the columns at design time if you do it
that way - you'll have type in all the details.  If you want design time
help consider using an ObjectDataSource that points to a method that returns
the list.

Nick

>I see that I can create a List<string> list and bind it to a GridView
>control (by setting the DataSource property to the list and calling the
[quoted text clipped - 6 lines]
>
> Thanks.
Jonathan Wood - 20 Jan 2008 03:31 GMT
Nick,

> You can just set the DataSource to be a List<MyObject> and DataBind. You
> won't get any help with setting up the columns at design time if you do it
> that way - you'll have type in all the details.  If you want design time
> help consider using an ObjectDataSource that points to a method that
> returns the list.

Yeah, that's how it's done with a List<string> so I thought that might work.
But, with my class defined like this:

public class ClientMenuItem
{
 public int MealNum;
 public float Substitutions;
 public string Group;
 public float Units;
 public string Measure;
 public string Description;
 public float Calories;
 public float Protein;
 public float Carbohydrate;
 public float Fat;
}

I tried the following:

  List<ClientMenuItem> items = ClientUsers.GetMenuItems(menus[0].MenuID);
  GridView1.DataSource = items;
  GridView1.DataBind();

And I get the following error at runtime on the last line above:

"The data source for GridView with id 'GridView1' did not have any
properties or attributes from which to generate columns.  Ensure that your
data source has content."

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Nick Bennett - 20 Jan 2008 04:00 GMT
The message is correct: your class doesn't have any properties - it has
public fields, which isn't a good thing.  Try replacing your fields with
properties - e.g. convert

   public int MealNum;

to

   public int MealNum { get; set; }

if you're in VS2008, or this

   private int _mealNum;

   public int MealNum
   {
       get { return _mealNum; }
       set { _mealNum = value; }
   }

   if in VS2005 or VS2003.

> Nick,
>
[quoted text clipped - 32 lines]
> properties or attributes from which to generate columns.  Ensure that your
> data source has content."
Jonathan Wood - 20 Jan 2008 04:18 GMT
That's the deal? I thought about that. Actually, I was putting all
properties in my classes until I decided it was WAAAAAAAAAAAAY too much
typing and just seemed unnecessary.

Yup, that's it! Nice. Cool feature.

Thanks.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

> The message is correct: your class doesn't have any properties - it has
> public fields, which isn't a good thing.  Try replacing your fields with
[quoted text clipped - 54 lines]
>> properties or attributes from which to generate columns.  Ensure that
>> your data source has content."
Milosz Skalecki [MCAD] - 20 Jan 2008 20:10 GMT
Howdy,

You don't need to type property declaration yourself. VS >= 2005 supports
refactoring and includes very useful commands to make your life easier:
Three ways to quickly implement a property:
1. Press Ctrl+K,X or go to main menu Edit->Intelli Sense->Insert Snippet
select "prop" or Visual C#\prop from drop down list, and change property's
type and name.
2. in the code type
private [AnyType] fieldName;
(Ctrl+R,E) or right click -> Refactor ->Encapsulate Field.
3. Create class diagram, and design your classes through a GUI.

Hope this helps
Signature

Milosz

> That's the deal? I thought about that. Actually, I was putting all
> properties in my classes until I decided it was WAAAAAAAAAAAAY too much
[quoted text clipped - 62 lines]
> >> properties or attributes from which to generate columns.  Ensure that
> >> your data source has content."
Jonathan Wood - 09 Feb 2008 04:30 GMT
Sorry for the slow delay but the Encapsulate Field command looks like the
one I was trying to find.

Thanks!

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

> Howdy,
>
[quoted text clipped - 82 lines]
>> >> properties or attributes from which to generate columns.  Ensure that
>> >> your data source has content."

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.