question

Saga-3468 avatar image
0 Votes"
Saga-3468 asked DSPatrick commented

How do I populate DataGridView from multiple resultsets

Hi all,

I have a DataGridView that is populated by the result of a stored procedure. The stored procedure returns a dataset with two data tables, one being the header and another the data. I populate the datagridview with the second data table. Here is the code:

 //Populate the gridview if a dataset was passed
 if (cPacket.dReportData != null)
       if (cPacket.dReportData.Tables.Count > 0)
       {
           //The report data is always in the last data table (if more than one).
           dvgData.DataSource = 
                 cPacket.dReportData.Tables[cPacket.dReportData.Tables.Count - 1];
    
           //Disable data grid sort behavior.
           foreach (DataGridViewColumn column in dvgData.Columns)
           {
               column.SortMode = DataGridViewColumnSortMode.NotSortable;           
            }
       }

This has worked because all the data that needs to go into the DataGridView has been in one data table.

Now I need to modify the stored procedure and more than one data table will be returned which need to go into the datagridview. I am working to see whether I can combine the sp results into one result set, but no luck so far.

The information that I've found so far assumes that there will be a separate DatGridView for each data table, but in my case, I need to combine all data tables into that one DataGridView.

I found content that describes how to merge two data tables. I am investigating this further to see whether it solves my dilemma:

https://www.c-sharpcorner.com/UploadFile/b182bf/how-to-add-two-datatable-in-single-datatable-in-C-Sharp/

Any recommendations? Thank you! Saga

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DSPatrick avatar image
1 Vote"
DSPatrick answered DSPatrick commented

I'd probably do a Union inside the stored proc.
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver15

--please don't forget to upvote and Accept as answer if the reply is helpful--





· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks DSPatrick.

Yes, in fact, I am using a union. The sp fetches data using 7 different queries and I use a UNION ALL to get back one data table. This was fine until I was asked to break one of the queries into sections and have a footer for each section.

To be more precise, this sp returns student data. I had one query to bring back all courses taken and another query to get the GPA; however, now they want the GPA for each semester. I don't want to get too much into detail because this is off-topic here, but right now I have a prototype working that returns two data tables (courses plus GPA footer) for each semester. So either I find a way to combine these result sets into one data table in the sp, or I work this at the client side and merge all of these data tables into one and use that as my grid's data source. :-) Saga

0 Votes 0 ·

Sounds kind of nasty, if you can't do it in the SP then maybe go for two different DGV's


0 Votes 0 ·

It is nasty. I'll have to think about it. I'll look into the possibility of using more than one DataGrid. Again, I appreciate your feedback, thanks! Saga

0 Votes 0 ·
Show more comments