Search code examples
c#.netdatagridviewc#-2.0bindingsource

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?


Solution

  • 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; }
      }
    }