How to Bind List<> to GridView with Custom Objects

Discussion in 'ASP .Net' started by Jonathan Wood, Jan 20, 2008.

  1. 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.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, Jan 20, 2008
    #1
    1. Advertising

  2. Jonathan Wood

    Nick Bennett Guest

    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



    "Jonathan Wood" <> wrote in message
    news:%...
    >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.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
     
    Nick Bennett, Jan 20, 2008
    #2
    1. Advertising

  3. 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."

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, Jan 20, 2008
    #3
  4. Jonathan Wood

    Nick Bennett Guest

    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.





    "Jonathan Wood" <> wrote in message
    news:...
    > 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."
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
     
    Nick Bennett, Jan 20, 2008
    #4
  5. 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.

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

    "Nick Bennett" <> wrote in message
    news:...
    > 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.
    >
    >
    >
    >
    >
    > "Jonathan Wood" <> wrote in message
    > news:...
    >> 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."
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>

    >
    >
     
    Jonathan Wood, Jan 20, 2008
    #5
  6. 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
    --
    Milosz


    "Jonathan Wood" wrote:

    > 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.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > "Nick Bennett" <> wrote in message
    > news:...
    > > 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.
    > >
    > >
    > >
    > >
    > >
    > > "Jonathan Wood" <> wrote in message
    > > news:...
    > >> 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."
    > >>
    > >> --
    > >> Jonathan Wood
    > >> SoftCircuits Programming
    > >> http://www.softcircuits.com
    > >>

    > >
    > >

    >
    >
     
    Milosz Skalecki [MCAD], Jan 20, 2008
    #6
  7. Sorry for the slow delay but the Encapsulate Field command looks like the
    one I was trying to find.

    Thanks!

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

    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > 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
    > --
    > Milosz
    >
    >
    > "Jonathan Wood" wrote:
    >
    >> 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.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
    >> "Nick Bennett" <> wrote in message
    >> news:...
    >> > 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.
    >> >
    >> >
    >> >
    >> >
    >> >
    >> > "Jonathan Wood" <> wrote in message
    >> > news:...
    >> >> 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."
    >> >>
    >> >> --
    >> >> Jonathan Wood
    >> >> SoftCircuits Programming
    >> >> http://www.softcircuits.com
    >> >>
    >> >
    >> >

    >>
    >>
     
    Jonathan Wood, Feb 9, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmVk?=

    Bind an Array of Custom Objects

    =?Utf-8?B?SmVk?=, Nov 10, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    3,036
    fd537
    Nov 22, 2004
  2. mr2_93
    Replies:
    1
    Views:
    3,678
    mr2_93
    Oct 2, 2005
  3. Steven Baggs
    Replies:
    5
    Views:
    43,456
    Steven Baggs
    Oct 18, 2005
  4. Giorgio
    Replies:
    0
    Views:
    3,567
    Giorgio
    Jan 9, 2006
  5. Replies:
    0
    Views:
    617
Loading...

Share This Page