简体   繁体   中英

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.

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)

  datagrid.ItemSource = lst;

try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM