简体   繁体   中英

How to set Datasource to a Datagridview Control in C#

This might look really simple, but I am not able to figure out how to do it. I am not an expert with Databinding in C#.

I have a list of Class objects(It is a nested Class) which looks something like this :

public class IntVector
{
    private string customerid;
    private string hash_id;
    private string client_name;
    private string mobile_no;
    private string address;

    //Table

    private List<CustomerInfo> customerinfo;
}

I have a list of IntVector

private List<IntVector> UserData;

Now how to set CustomerInfo as the Datasource for a DatagridView Control which is member of the list UserData.

Thanks

First, you have to expose your customerinfo list somehow (it is now private, so you can't get it from outside your IntVector class).

If it was public:

BindingSource bs = new BindingSource(); 

int indexInUserDataList = 0;
bs.DataSource = UserData[indexInUserDataList].customerinfo;

datagridview.DataSource = bs;

Also, you may want to consider using BindingList instead of List if you want to modify your list programmatically and want those changes to be propagated to the control (here the difference is explained List<T> vs BindingList<T> Advantages/DisAdvantages )

What your CustomerInfo class looks like? I assume you want to bind columns of DataGridView to public properties of CustomerInfo class, for exsample:

class CustomerInfo 
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Address {get;set;}

   private string somePrivateData;
}

Now if AutoGenerateColumns in your DataGridView is set to true, then 3 columns "Id", "Name" and "Address" will be automatically created in your DataGridView. "somePrivateData" will be ignored.

If you want to define collumns yourself, you can do it like this:

// make sure to do it before binding DataGridView control
datagridview.AutoGenerateColumns = false;

DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "Name";
col1.HeaderText = "Customer name";
col1.Name = "column_Name";
datagridview.Columns.Add(col1);

DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.DataPropertyName = "Address";
col2.HeaderText = "Address";
col2.Name = "column_Address";
datagridview.Columns.Add(col2);

You need to set the private list of customers as public:

public class IntVector
{
    private string customerid;
    private string hash_id;
    private string client_name;
    private string mobile_no;
    private string address;

    //Table

    public List<CustomerInfo> customerinfo;

}

private List<IntVector> UserData;

//Populate the UserData list here

And then you can set the datasource to DataGridView like:

DataGridView.DataSource = UserData[0].customerinfo;

I hope that helps...

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