Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Below code is used to set Parent-child relationship between 2 tables and display through 2 dataGridViews.
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; database= NORTHWND; Persist Security Info=false; Integrated Security=true;"))
            {
                SqlCommand cmd1 = new SqlCommand();
                SqlCommand cmd2 = new SqlCommand(); 
                cmd1 = con.CreateCommand();
                cmd2 = con.CreateCommand();
                cmd1.CommandText = "Select * from Customers";
                cmd2.CommandText = "Select * from Orders";
                cmd1.Connection = con;
                cmd2.Connection = con;

                con.Open();
                    SqlDataAdapter adp1 = new SqlDataAdapter(cmd1);
                    DataSet ds = new DataSet();
                    adp1.Fill(ds, "Customers");
                    SqlDataAdapter adp2 = new SqlDataAdapter(cmd2); 
                    adp2.Fill(ds, "Orders");
                DataRelation relation = new DataRelation("CustomersOrders", 
                    ds.Tables["Customers"].Columns["CustomerID"], 
                    ds.Tables["Orders"].Columns["CustomerID"]); 
                    ds.Relations.Add(relation);  // add relation 
                    dataGridView1.DataSource = ds; 
                    dataGridView1.DataMember = "Customers";
                    dataGridView2.DataSource = ds; 
                    dataGridView2.DataMember = "CustomersOrders"; // this line shows error 
            }


Code shows following error message on execution, for the last line of code:
“Childlist for field “CustomerOrders” can’t be created.”
What is the reason and what should be the solution?
Posted

Hi,
Have you tried setting the DataMember to "Customers.CustomersOrders". As far as I know that's the correct syntax.
Regards
 
Share this answer
 
Comments
sk saini 6-Jun-11 9:34am    
Thanks, its working perfectly.
This can also happen if you're debugging and you have the DB open at the same time.
 
Share this answer
 
Comments
sk saini 6-Jun-11 9:01am    
OK, but DB is closed.
See Here[^] it may help
 
Share this answer
 
I think this error is raised because of, you haven't filled CustomersOrders Table into DataSet object... and you are trying to access it.....
To apply DataMember in dataGridView, the table(DataMember Name) must be provided as DataSource...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900