using System;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace shujuku
{
public partial class Form1 : Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components=null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if(disposing)
{if(components!=null)
{
components.Dispose();
} }
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{this.dataGrid1=new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout(); this.dataGrid1.DataMember="";
this.dataGrid1.Dock=System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor=System.Drawing.SystemColors.ControlText;
this.dataGrid1.Name="dataGrid1";
this.dataGrid1.Size=new System.Drawing.Size(265,205);
this.dataGrid1.TabIndex=0;
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(264,205);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.dataGrid1});
this.Name="Form1";
this.Text="Form1";
this.Load+=new System.EventHandler(this.form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender,System.EventArgs else)
{
string conn =@ "uid=sa;pwd=;server=local;database=test";
SqlConnection myconn = new SqlConnection(conn);
myconn.open(); DataSet ds=new DataSet();
SqlDataAdapter myAdapter=new SqlDataAdapter("select orderID as 订单号,EmployeeID as 雇员号,CustomerID as客户号,orderData as 订单日期 form order",myconn); myAdapter.Fill(ds,"order");
SqlDataAdapter myAdapter1=new SqlDataAdapter("select orderID as 订单号,ProductID as 货品号,UnitPrice as 单价,Quantity as 数量,Discount as 折扣 form [order Details]",myconn);
myAdapter1.Fill(ds,"order Details");
myconn.Close();
dataGrid1.DataSource=ds.Tables["orders"];
ds.Table["orders"].PrimaryKey=new DataColumn[]{ds.Tables["orders"].ColumnStyle["订单号"]};
DataRelation relationOrderID;
relationOrderID=ds.Relations.Add("单击查看明细",ds.Tables["order"].Columns["订单号"],ds.Tables["order Details"].Columns["订单号"]);
dataGrid1.Refresh();}}}
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace shujuku
{
public partial class Form1 : Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components=null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if(disposing)
{if(components!=null)
{
components.Dispose();
} }
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{this.dataGrid1=new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout(); this.dataGrid1.DataMember="";
this.dataGrid1.Dock=System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor=System.Drawing.SystemColors.ControlText;
this.dataGrid1.Name="dataGrid1";
this.dataGrid1.Size=new System.Drawing.Size(265,205);
this.dataGrid1.TabIndex=0;
this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(264,205);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.dataGrid1});
this.Name="Form1";
this.Text="Form1";
this.Load+=new System.EventHandler(this.form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender,System.EventArgs else)
{
string conn =@ "uid=sa;pwd=;server=local;database=test";
SqlConnection myconn = new SqlConnection(conn);
myconn.open(); DataSet ds=new DataSet();
SqlDataAdapter myAdapter=new SqlDataAdapter("select orderID as 订单号,EmployeeID as 雇员号,CustomerID as客户号,orderData as 订单日期 form order",myconn); myAdapter.Fill(ds,"order");
SqlDataAdapter myAdapter1=new SqlDataAdapter("select orderID as 订单号,ProductID as 货品号,UnitPrice as 单价,Quantity as 数量,Discount as 折扣 form [order Details]",myconn);
myAdapter1.Fill(ds,"order Details");
myconn.Close();
dataGrid1.DataSource=ds.Tables["orders"];
ds.Table["orders"].PrimaryKey=new DataColumn[]{ds.Tables["orders"].ColumnStyle["订单号"]};
DataRelation relationOrderID;
relationOrderID=ds.Relations.Add("单击查看明细",ds.Tables["order"].Columns["订单号"],ds.Tables["order Details"].Columns["订单号"]);
dataGrid1.Refresh();}}}
获取是表order myAdapter.Fill(ds,"order");
填充也是orderSqlDataAdapter myAdapter1=new SqlDataAdapter("select orderID as 订单号,ProductID as 货品号,UnitPrice as 单价,Quantity as 数量,Discount as 折扣 form [order Details]",myconn);
myAdapter1.Fill(ds,"order Details");
myconn.Close(); dataGrid1.DataSource=ds.Tables["orders"];
这里是orders,多了一个s,能不疯吗,要细心一些,事勿忙,忙多错.