Joe Mo
Joe Mo

Reputation: 135

Datagrid in wpf - no binding required

I have a datagrid in wpf and a list of structs that I would like to display only some of its properties in the grids.

For example

public struct Person
{
   public int age;
   public string name;
   public string hobby;
} 

private List<Person> lst=new List<Person>();

I would like to display only name and age in the datagrid. How can I do that in a loop ?

I do this

foreach(Person p in lstp)
{ 
   datagrid.Items.Add(p.name);
} 

But nothing is displayed.

Upvotes: 1

Views: 957

Answers (2)

Artem Makarov
Artem Makarov

Reputation: 874

  datagrid.ItemSource = lst;

try this.

sorry, lost the idea.... Just mark the property you don't want by [Browsable(false)] attribute

Upvotes: 0

brunnerh
brunnerh

Reputation: 185445

Your struct contains no properties, only fields. Also you end up adding strings directly.

You could add anonymous objects, they use properties:

...Add(new { Name = p.name, Age = p.age })

(Alternatively you can add properties to your struct and add those directly. If you have more properties than you want to show just create the columns manually, turning AutoGenerateColumn off)

Upvotes: 3

Related Questions