从List<>加载数据到datagridview customerlist

时间:2013-05-13 00:35:47

标签: c# datagridview

我需要帮助填充DataGridView。当我调试时,我可以看到它有记录,但它们没有显示在DataGridView中。这是我的代码(请注意,我是C#的新手):

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();
}

1 个答案:

答案 0 :(得分: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();
}
相关问题