using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class DataGridRelation : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
public DataGridRelation()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(292, 266);
this.dataGrid1.TabIndex = 0;
//
// DataGridRelation
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Name = "DataGridRelation";
this.Text = "DataGridRelation";
this.Load += new System.EventHandler(this.DataGridRelation_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new DataGridRelation());
}
const string connstr = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
private DataSet ds = null;
private void DataGridRelation_Load(object sender, System.EventArgs e)
{
ds = new DataSet();
using ( SqlConnection conn = new SqlConnection( connstr ) )
{
conn.Open();
SqlDataAdapter da1 = new SqlDataAdapter( "select * from developers", conn );
da1.Fill( ds, "developers" );
SqlDataAdapter da2 = new SqlDataAdapter(
"SELECT empno, name " +
"FROM languages",
conn );
da2.Fill( ds, "languages" );
ds.Relations.Add( "language",
ds.Tables[ "developers" ].Columns[ "empno" ],
ds.Tables[ "languages" ].Columns[ "empno" ] );
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "developers";
}
}
}
|