0
votes

I am returning a dynamic dataset that contains multiple datatables to a dynamically generated grid view. I databind each datatable individually using the code below.

// Create the grid views for each one that was returned
        foreach (System.Data.DataTable table in datset.Tables)
        {
            WebCommon.Controls.GridView view = new WebCommon.Controls.GridView();
            ReportContainerPanel.Controls.Add(view);
            view.AutoGenerateColumns = true;
            view.ShowHeaderWhenEmpty = true;
            view.ShowHeader = true;
            view.ID = table.TableName;
            view.Size = WebCommon.Enums.TableSize.Small;
            view.DataSource = table;
            view.DataBind();

            // Add the spacer
            Literal spacer = new Literal();
            spacer.Text = "<br/>";
            ReportContainerPanel.Controls.Add(spacer);
        }

But i am getting this exception if the datatable being returned has columns but 0 rows.

The data source for GridView with id 'Table4' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

I know i can prevent this by checking for row count and then excluding that one from being printed but it is useful for the user to see that no data was returned for the grid. So i would like an empty grid with header columns to display. Is there way around this error?

EDIT this problem seems to actually be because the empty result in question has only 1 column of type varbinary and the gridview does not like that at all.

1

1 Answers

0
votes

Just check for an empty set,

If the set is empty, add a blank row then continue

    foreach (System.Data.DataTable table in datset.Tables)
    {
        if(table.Rows.Count == 0)
        {
            <code to add a new row>
        }//at this point the rest of the operations should work on the table

        WebCommon.Controls.GridView view = new WebCommon.Controls.GridView();
        ReportContainerPanel.Controls.Add(view);
        view.AutoGenerateColumns = true;
        view.ShowHeaderWhenEmpty = true;
        view.ShowHeader = true;
        view.ID = table.TableName;
        view.Size = WebCommon.Enums.TableSize.Small;
        view.DataSource = table;
        view.DataBind();

        // Add the spacer
        Literal spacer = new Literal();
        spacer.Text = "<br/>";
        ReportContainerPanel.Controls.Add(spacer);
    }

How to create a new row for a dataset