This is a migrated thread and some comments may be shown as answers.

Issue binding to DataSet with two Tables

1 Answer 96 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Adrian
Top achievements
Rank 1
Adrian asked on 19 Jun 2011, 12:12 AM
Hello,
I have a dataset containing two tables. One of the tables is the Customer table and the other one is the customer Purchases table. They have a one to many relationship; each customer can have more than one purchases.
I can't get the RadTreeView to dispay anything related to the Customer or Purchases. They only thing the TreeView displays is the following: System.Data.DataViewManagerListTypeDescriptor. That looks like an error message!?
What am I doing wrong?
I've been struggling with this for an entire day; trying anythign under the sun.
I am using the newest RadTreeView control.

My datbase tables look as follows:

Customer table
====================
ID (autoincrement unique ID)      
CustomerName

Purchases table
=============
PurchasesID (autoincrement unique ID)
Customer_ID (foreign key to ID in Customer table)

In the tree view, the customer's purchases should show up underneath each customer's name.
Here is my code:

private void frmMain_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataRelation relation;
            string connectionString = "server=MYSQLSERVER;database=MyStore;uid=FakeUserID;pwd=FakePassword";
            SqlConnection mySqlConnection = new SqlConnection(connectionString);
  
            string strCustomer_Query = "Select * from Customer";
            string strPurchases_Query = "Select * from Purchases";
  
            try
            {
                SqlCommand sql_Customer = new SqlCommand(strCustomer_Query, mySqlConnection);
                SqlDataAdapter da = new SqlDataAdapter(sql_Customer);
  
                da.Fill(ds, "Customer");
                da.SelectCommand.CommandText = strPurchases_Query;
                da.Fill(ds, "Purchases");
  
                // Add the relation. Maybe this is not needed because we use the RelationBindings of the RadTreeView
                DataColumn parentCol = ds.Tables["Customer"].Columns["ID"];
                DataColumn childCol = ds.Tables["Purchases"].Columns["Customer_ID"];
                relation = new DataRelation("Customer_Purchases", parentCol, childCol);
                ds.Relations.Add(relation);
  
               treeView1.ShowLines = true;
               treeView1.RelationBindings.Add(new RelationBinding(ds, "CustomerName", "Customer_ID", "PurchaseID", "ID"));
               treeView1.DataSource = ds;
               treeView1.DisplayMember = "CustomerName";
               treeView1.ValueMember = "ID";
               treeView1.ParentMember = "ID";
               treeView1.ChildMember = "PurchaseID";
               treeView1.ExpandAll();
}


Please help.
Thank you!

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 22 Jun 2011, 12:53 PM
Hello Adrian,

You can use this changed code snippet in your application:

try
    {
        SqlCommand sql_Customer = new SqlCommand(strCustomer_Query, mySqlConnection);
        SqlDataAdapter da = new SqlDataAdapter(sql_Customer);
 
        da.Fill(ds, "Customer");
        da.SelectCommand.CommandText = strPurchases_Query;
        da.Fill(ds, "Purchases");
     
 
        treeView1.ShowLines = true;
        treeView1.DataSource = ds.Tables["Customer"];
        treeView1.DisplayMember = "CustomerName";
        treeView1.ValueMember = "ID";
 
        treeView1.RelationBindings.Add(ds.Tables["Purchases"], "PurchasesID", "ID", "Customer_ID ", "PurchasesID");
        treeView1.ExpandAll();
    }

Please review the example for DataRelation in our Demo application and the article in our online documentation for more details of the new API.

All the best,
Julian Benkov
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
Treeview
Asked by
Adrian
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or