2013-05-13 96 views
0

我需要幫助填充DataGridView。當我調試時,我可以看到它有記錄,但它們沒有顯示在DataGridView中。這裏是我的代碼(請注意,我是C#中的新手):將數據從List <>加載到datagridview中customerlist

private void listCustomer_Frm_Load(object sender, EventArgs e) 
{ 
    DataGridView custDGV = new DataGridView(); 
    customerList = CustomerDB.GetListCustomer(); 
    custDGV.DataSource = customerList; 
    cm = (CurrencyManager)custDGV.BindingContext[customerList]; 
    cm.Refresh(); 
} 

回答

2

您在函數範圍創建DataGridView,並且永遠不會將其添加到任何容器。由於沒有提及它,只要函數退出就會消失。

你需要做的是這樣的:該函數完成

this.Controls.Add(custDGV); // add the grid to the form so it will actually display 

之前。像這樣:

private void listCustomer_Frm_Load(object sender, EventArgs e) 
{ 
    DataGridView custDGV = new DataGridView(); 
    this.Controls.Add(custDGV); // add the grid to the form so it will actually display 
    customerList = CustomerDB.GetListCustomer(); 
    custDGV.DataSource = customerList; 
    cm = (CurrencyManager)custDGV.BindingContext[customerList]; 
    cm.Refresh(); 
} 
+1

要麼是這樣,要麼@Salsero已經在某個表單的某個DataGridView中,只需要填充它而不是創建一個新的 – joshuahealy 2013-05-13 03:21:51

相關問題