TOP KEK
TOP KEK

Reputation: 2661

Bind Bindingsource to a list

I have a class like

internal class CalculationsDataRelations
{
    public List<CalculationsDataRelation> Relations;
}

And trying to bind it to a datagridview using following code

   relations = new CalculationsDataRelations();
   bs = new BindingSource(relations, "Relations");
   DgvRelations.DataSource = bs;

But I get exception "DataMember property 'Relations' cannot be found on the DataSource."

How to bind datagridview properly?

Upvotes: 2

Views: 4637

Answers (1)

LarsTech
LarsTech

Reputation: 81675

Binding has to happen with Properties, but your internal class is only providing a Field. Also, you haven't instantiated the List<CalculationsDataRelation> variable with "new".

Try changing it to something like this:

internal class CalculationsDataRelations {
  private List<CalculationsDataRelation> relations = new List<CalculationsDataRelation>();

  public List<CalculationsDataRelation> Relations {
    get { return relations; }
  }
}

Upvotes: 1

Related Questions