1
votes

I have a stored procedure that returns 10 columns [column A, column B, etc].

Based off of that table, a winform ListBox is created listing those columns.

The items in the ListBox can be selected, then a "submit" button, which opens up a new Form with a DataGridView on it.

In the new Forms load event, I have placed code that iterates through the column collection and creates the structure of the DataGridView.

The columns I select show up, but the data is not populated into the returned rows.

   StoredProcedures.up_Report_Adapter reportAdapter = new StoredProcedures.up_Report_Adapter();


        gridCustomData.AutoGenerateColumns = false;

        gridCustomData.DataSource = reportAdapter.GetData();

        gridCustomData.Columns.Clear();
        gridCustomData.Columns.Add(new DataGridViewColumn() { HeaderText = "REPORT_ID", CellTemplate = new DataGridViewTextBoxCell() });

    foreach (var item in customReport.lstBoxSelectFields.SelectedItems)
            {
                gridCustomData.Columns.Add(item.ToString(), item.ToString());
            }

        gridCustomData.Refresh();

The right number of rows show up for the data, and if I put a break in at the gridCustomData.Refresh(), I can see that complete data for the columns selected from the ListBox.

So, how do I get the data to actually appear per defined row/column?

I have tried change the order of when the columns are created to before and after the gridCustomData DataSource is updated with the same results.

I also tried creating a BindingSource and using it as the DataSource for the DataGridView, then making the DataSource for the BindingSource the populated table adapter. The data is still not visible.

The goal is to create a "customized query", but really, it just means selecting the columns of what is in the DataSet. I am fetching the entire DataSet returned by the stored procedure (which itself is limited by parameters).

As a side note, if anyone knows of a thorough cookbook for these types of things, I would love to know what its title is. The API data from MSDN is useless as it uses contrived examples.

1

1 Answers

2
votes

Bind to your datasource after setting up the columns.

First set the DataPropertyName for the column DataPropertyName = "REPORT_ID"

gridCustomData.Columns.Add(new DataGridViewColumn() { HeaderText = "REPORT_ID", CellTemplate = new DataGridViewTextBoxCell(), DataPropertyName = "REPORT_ID" });

Then bind to the datasource. gridCustomData.DataSource = reportAdapter.GetData();