Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want better soloution to work with relations in data binding..thank u



Here i have to display the two tables in two gridviews and their details when i enter department name in combo box..

but here while adding relation and inserting dataset to gridview it's ok but when i insert dataMember for grid view it show child field can't be created for the dataset.

dataGridView1.DataSource = dtviewMang;

dataGridView1.DataMember = "EmpDs.relEmpDetails";

here i have taken simple Sql Table -- employee as Emp and Department as Dept and DepartmentDetails as deptDetails.

please solve the problem and if u got the answer please paste it down here..

Thank You..











C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Collections;
using System.Data.SqlClient;


namespace WinForm1
{
    public partial class SampleProg2 : Form
    {
        private String ConnectionString;
        private DataViewManager dtviewMang;
        private DataSet ds;

        public SampleProg2()
        {
            InitializeComponent();

            ConnectionString = "Data Source=ETG-75;Initial Catalog=test;Integrated Security=True";
            SqlConnection con = new SqlConnection(ConnectionString);

            ds = new DataSet("EmpDs");
             

            SqlDataAdapter da1 = new SqlDataAdapter("select * from Emp", con);
            da1.TableMappings.Add("Table", "Emp");
            da1.Fill(ds);

            SqlDataAdapter da2 = new SqlDataAdapter("select * from Dept", con);
            da2.TableMappings.Add("Table", "Dept");
            da2.Fill(ds);


            SqlDataAdapter da3 = new SqlDataAdapter("select * from DeptDetails", con);
            da3.TableMappings.Add("Table", "DeptDetail");
            da3.Fill(ds);
            
         
            //show created table names with in the dataset
            string myMessage = "Table Mappings";
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                myMessage += i.ToString() + " " + ds.Tables[i].ToString()+" ";
            
            }
             
            //Establish the relation between Employee  and dept tables

            System.Data.DataRelation relEmpDetails;
            //  System.Data.DataColumn colemp1;
            System.Data.DataColumn colDept1;
            System.Data.DataColumn colemp1 = ds.Tables[0].Columns["Deptno"];
            colDept1 = ds.Tables[1].Columns["Deptno"];
            relEmpDetails = new System.Data.DataRelation("RelEmpDetails", colDept1, colemp1 );
            ds.Relations.Add(relEmpDetails);


            System.Data.DataRelation relDetpDetails;
            System.Data.DataColumn colDept2;
            System.Data.DataColumn colDeptdetail2;
           colDept2 = ds.Tables[1].Columns["Deptno"];
            colDeptdetail2 = ds.Tables[2].Columns["deptno"];
            relDetpDetails = new System.Data.DataRelation("RelDeptDetails", colDept2, colDeptdetail2  );
            ds.Relations.Add(relDetpDetails);

            //show created relations with in the dataset

            myMessage += "Relations Mappings:";
            for (int i = 0; i < ds.Relations.Count; i++)
            {
                myMessage += i.ToString() + " " + ds.Relations[i].ToString() + " ";

            
            }
            textBox1.Text = myMessage;

            // The DataViewManager returned by the DefaultViewManager
            // property allows you to create custom settings for each
            // DataTable in the DataSet.
            dtviewMang = ds.DefaultViewManager;

Error shows up here:
C#
dataGridView1.DataSource = dtviewMang;

dataGridView1.DataMember = "EmpDs.relEmpDetails";//Error Here

dataGridView2.DataSource = dtviewMang;

dataGridView2.DataMember = "EmpDs.RelEmpDetails.RelDeptDetails"; // Error here

comboBox1.DataSource = dtviewMang;
comboBox1.DisplayMember = "EmpDs.DName";
comboBox1.ValueMember = "EmpDs.Dno"; //error here

C#
            textBox2.DataBindings.Add("Text", dtviewMang, "EmpDs.Loc");
            textBox3.DataBindings.Add("Text", dtviewMang, "EmpDs.Comments");


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.BindingContext[dtviewMang, "EmpDs"].Position > 0)
            {
                this.BindingContext[dtviewMang, "EmpDs"].Position--;
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            CurrencyManager cm = (CurrencyManager)this.BindingContext[dtviewMang, "EmpDs"];
            if (cm.Position < cm.Count - 1)
            {
                cm.Position++;
            }

        }

       
    }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 27-Sep-11 20:41pm
v3

Dear fellow,

Why not you try linq. Take a look at this article. I hope it will do. This basically relates to Entity framework but the answer to your question exists in this article.

Introduction to the Entity Framework[^]
 
Share this answer
 
//suppose Fill_Details_By_Dept(); is a function return a
DataSet ds which contains 2 tables ,
1 ds.tables[0] conatins your req 2 columns
2.ds.tables[1] conatins your req 1 columns

@ suppose your grid hai 3 columns and your grid name is grd then
C#
ds2 = Fill_Details_By_Dept();
                      
      for (int x = 0; x < ds.Tables[2].Rows.Count; x++)
                {
                    grd.Rows[x].Cells[0].Value = ds.Tables[0].Rows[x].ItemArray[0];
                    grd.Rows[x].Cells[0].Value = ds.Tables[0].Rows[x].ItemArray[1];
                    grd.Rows[x].Cells[1].Value = ds.Tables[1].Rows[x].ItemArray[0];


so your grid populate with the result you want....


enjoy :)
 
Share this answer
 
Comments
V G S Naidu A 28-Sep-11 2:39am    
Thank u
You seem to be trying to set the data source of the grid to a relation. Grids expect to be given a table or view (or other non-System.Data things like arrays, lists, IEnumerables etc that aren't relevant here). Try 'EmpDs.Emp', or just set the DataSource to the table directly:

dataGridView1.DataSource = ds.Tables["Emp"];
 
Share this answer
 
Comments
V G S Naidu A 28-Sep-11 2:39am    
thank u

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