How do I populate DataGridView from multiple resultsets

Saga 461 Reputation points
2021-10-27T20:19:21.833+00:00

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

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-10-27T20:27:08.297+00:00

    I'd probably do a Union inside the stored proc.
    https://learn.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--

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.