Fulano
Fulano

Reputation: 31

DataBinding to a DataGrid Not Working

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();

    List<Person> lista = new List<Person>();
    lista.Add(new Person(1, "Joao", 50.0f));
    lista.Add(new Person(2, "Maria", 150.0f));

    dataGrid1.ItemsSource = lista;
  }

  public class Person
  {
    public int id;
    public string name;
    public float salary;

    public Person(int id, string name, float salary)
    {
      this.id = id;
      this.name = name;
      this.salary = salary;
    }
  }
}

Upvotes: 1

Views: 51

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064004

Binding is generally to properties, not fields:

public int Id {get;set;}
public string Name {get;set;}
public decimal Salary {get;set;}

public Person(int id, string name, decimal salary)
{
    Id = id;
    Name = name;
    Salary = salary;
}

note also - Salary should certainly be a decimal (not a float).

If you find you can't create new rows of Person records, try adding a parameterless constructor:

public Person() { Name = ""; }

Upvotes: 1

Related Questions