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..
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);
string myMessage = "Table Mappings";
for (int i = 0; i < ds.Tables.Count; i++)
{
myMessage += i.ToString() + " " + ds.Tables[i].ToString()+" ";
}
System.Data.DataRelation relEmpDetails;
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);
myMessage += "Relations Mappings:";
for (int i = 0; i < ds.Relations.Count; i++)
{
myMessage += i.ToString() + " " + ds.Relations[i].ToString() + " ";
}
textBox1.Text = myMessage;
dtviewMang = ds.DefaultViewManager;
Error shows up here:
dataGridView1.DataSource = dtviewMang;
dataGridView1.DataMember = "EmpDs.relEmpDetails";
dataGridView2.DataSource = dtviewMang;
dataGridView2.DataMember = "EmpDs.RelEmpDetails.RelDeptDetails";
comboBox1.DataSource = dtviewMang;
comboBox1.DisplayMember = "EmpDs.DName";
comboBox1.ValueMember = "EmpDs.Dno";
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]