2013-05-13 57 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

相关问题