Issue binding to DataSet with two Tables

2 posts, 0 answers
  1. Adrian
    Adrian avatar
    6 posts
    Member since:
    May 2009

    Posted 18 Jun 2011 Link to this post

    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!
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 22 Jun 2011 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top