Salsero
Salsero

Reputation: 19

loading data into datagridview from List<> customerlist

I need help populating a DataGridView. When I debug I can see that it has records, but they are not shown in the DataGridView. Here is my code (mind you I am a newbie in 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();
}

Upvotes: 0

Views: 1130

Answers (1)

Paul Sasik
Paul Sasik

Reputation: 81469

You're creating the DataGridView at function scope and you never add it to any container. Since nothing has a reference to it, it disappears as soon as the function exits.

You need to do something like this:

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

Before the function finishes. Like this:

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

Upvotes: 2

Related Questions