首先,你必須以某種方式暴露你的customerinfo列表(現在是私人的,所以你不能從你的intVector的類的外部得到它)。
如果它是市民:
BindingSource bs = new BindingSource();
int indexInUserDataList = 0;
bs.DataSource = UserData[indexInUserDataList].customerinfo;
datagridview.DataSource = bs;
此外,你可能想,如果你想通過程序修改列表,並希望這些更改傳播到控制(這裏的差異考慮使用,而不是名單的BindingList解釋爲List<T> vs BindingList<T> Advantages/DisAdvantages)
你的CustomerInfo類是什麼樣的?我想你想的DataGridView的列綁定到CustomerInfo類的公共屬性,exsample:
class CustomerInfo
{
public int Id {get;set;}
public string Name {get;set;}
public string Address {get;set;}
private string somePrivateData;
}
現在,如果你的DataGridView的AutoGenerateColumns設置爲true,那麼3列「ID」,「名稱」和「地址「將自動在你的DataGridView中創建。 「somePrivateData」將被忽略。
如果你想自己定義collumns,你可以做這樣的:
// 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_Address";
datagridview.Columns.Add(col1);
DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
col2.DataPropertyName = "Address";
col2.HeaderText = "Address";
col2.Name = "column_Name";
datagridview.Columns.Add(col2);
http://stackoverflow.com/questions/1228539/how-to-bind-list-to-datagridview – phadaphunk 2013-04-04 12:35:37
我很抱歉,我不明白的帖子和解決方案。你能否詳細說明一下?謝謝 – Kiran 2013-04-04 12:48:51