Solved

Show results from multiple DataTable in one DataGridView

Posted on 2007-04-04
2
3,294 Views
Last Modified: 2012-05-05
I have a typed DataSet that contains two tables, related through key:
Table: People
ID FirstName LastName CompanyID

Table: Companies
ID Name

Key: People.ID -> Companies.ID

Note: DataSet is loaded from XML file, i.e. there is no database.

Problem:
I want to show data table (using DataGridView) with following columns:
First Name | Last Name | Company Name

What are my options?
Should I have a database, I could run an SQL query and show the results, but I don't have a database.
0
Comment
Question by:rgavrilov
  • 2
2 Comments
 
LVL 5

Accepted Solution

by:
kosturdur earned 50 total points
ID: 18855316
I also want to learn your questions answer. While I'm waiting for experts to answer your question, I found some information and want to share with you. There must be easier ways but;

1) You could loop two tables and build a new datatable. And use it.
2) You can create relation between the tables like;

 CompaniesDS.Relations.Add("myRelation",
    CompaniesDS.Tables["People"].Columns["CompanyID"],       // column in parent table
    CompaniesDS.Tables["Companies"].Columns["ID"]);      // column in child tablle

.....or

DataColumn primarykey = dataset.Tables["People"].Columns["CompanyID"];
DataColumn foreignkey = dataset.Tables["Companies"].Columns["ID"];
DataRelation myRelation = dataset.Relations.Add(primarykey, foreignkey);

.............you could use ListView Control and do something like;

DataSet1TableAdapters.PeopleTableAdapter peopleAdapter =
    new DataSet2TableAdapters.PeopleTableAdapter();
DataSet1.PeopleDataTable peopleDataTable;
peopleDataTable = peopleAdapter.GetData();
myListView.Items.Clear();
for (int i = 0; i < peopleDataTable.Rows.Count; i++)
{
  DataRow drow = peopleDataTable.Rows[i];
  ListViewItem lvi = new ListViewItem(drow["First_Name"].ToString());
  lvi.SubItems.Add(drow["Last_Name"].ToString());
   lvi.SubItems.Add(DataSet2.Relations["myRelation"].ChildColumns["Company_Name"].ToString());
   myListView.Items.Add(lvi);
}
Kosturdur
0
 
LVL 5

Expert Comment

by:kosturdur
ID: 18855405
Sorry all DataSet2's must be DataSet1.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now