Multiple result sets in one DataGridView

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am wondering if someone could provide a code sample (or just a suggested
approach) on how to fill a single DataGridView with multiple result sets. For
example, this statement returns a one-line result set for each table in the
database:

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

It would be handy to be able to put them all into one DataGridView. I would
like to be able to handle an arbitrary SQL statement; above is just one
example. The code I am currently using is:
----------------------------------------------
DataTable dataTable = new DataTable();
SqlDataAdapter aviAdapter = new SqlDataAdapter(query, connection);
aviAdapter.Fill(dataTable);
aviDataTableBindingSource.DataSource = dataTable;
aviDataTableBindingSource.DataMember = null;
aviDataGridView.DataSource = aviDataTableBindingSource;
 
Hi Michael ,

Based on my understanding, you want to display multiple datatables data in
a single DataGrid.

To achieve this effect, we have several ways, I will list some below:
1. Use a JOIN helper class to join the 2 tables together, then bind it to
the DataGrid. The 2 KBs below show the joining at DataTable level and
DataView level:
"HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;326080
"HOW TO: Implement a Custom DataView Class in Visual Basic .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;325682
2. We can extend the binding expression to recognize another table's
binding field at DataGridTextBoxColumn UI level, see the KB below:
"HOW TO: Extend the Windows Form DataGridTextBoxColumn to Display Data From
Other Tables by Using Visual C# .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;319076

Although they are intended for DataGrid, however, the principle is the same
for DataGridView. Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ok, if you need further help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top