Lakhae
Lakhae

Reputation: 1899

How to find the number of columns in the bindingsource?

I am trying to create dynamic table based on the bindingsource. My datasource is bind with the bindingsource. So, How can I know number of columns beforehand while creating a table?

Or is there any other solution?

Upvotes: 0

Views: 1528

Answers (1)

Dmitry
Dmitry

Reputation: 14059

Try to use the BindingSource.GetItemProperties method.
For instance, let's assume that the following class represents a single table row (as well as a single BindingSource item):

public class Item
{
    public int A { get; set; }
    public string B { get; set; }
}

Since it has two public properties, so the BindingSource will have two columns.

List<Item> list = new List<Item>();
BindingSource bindingSource = new BindingSource { DataSource = list };

You can get the number of columns in it as follows:

int columnsCount = bindingSource.GetItemProperties(null).Count;  // returns 2

Upvotes: 2

Related Questions