0 Hi Shaik,
Try this...
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.OleDb;
namespace Data_relation
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
DataRelation dr;
string str;
public Form1()
{
InitializeComponent();
}
private void btndisplay_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from product";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "product");
str = "select * from customer";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
oda.Fill(ds, "customer");
con.Close();
DataColumn dc1 = new DataColumn();
DataColumn dc2 = new DataColumn();
dc1 = ds.Tables["product"].Columns["prodid"];
dc2 = ds.Tables["customer"].Columns["prodid"];
dr = new DataRelation("Customers taking this product are", dc1, dc2);
ds.Relations.Add(dr);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "product";
}
}
}
Thanks
If this post helps you mark it as answer
-1 Check this code:
My code does not connect to database, but building two datatables and joining them using datarelation.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
DataTable salesTable = new DataTable();
DataTable productTable = new DataTable();
// This information is cosmetic, only.
salesTable.TableName = "Sales";
productTable.TableName = "Products";
// Add the tables to the DataSet:
dataSet.Tables.Add(salesTable);
dataSet.Tables.Add(productTable);
dataSet.Relations.Add("myrelation",
dataSet.Tables["Sales"].Columns["ID"],
dataSet.Tables["Products"].Columns["ID"]);
}
private static DataTable GetCustomers()
{
// Create sample Customers table.
DataTable table = new DataTable();
table.TableName = "Sales";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
table.Columns.Add("CustomerName", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Mary" });
table.Rows.Add(new object[] { 1, "Andy" });
table.Rows.Add(new object[] { 2, "Peter" });
table.AcceptChanges();
return table;
}
private static DataTable GetProducts()
{
// Create sample Products table.
DataTable table = new DataTable();
table.TableName = "Products";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID",
typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Wireless Network Card" });
table.Rows.Add(new object[] { 1, "Hard Drive" });
table.Rows.Add(new object[] { 2, "Monitor" });
table.Rows.Add(new object[] { 3, "CPU" });
table.AcceptChanges();
return table;
}
}
Let me know if you have any questions.