0

我需要帮助填充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();
}
4

1 回答 1

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();
}
于 2013-05-13T00:40:17.483 回答