背景:
vs2003
WinForm有2个表:部门,员工,要在1个DataGrid中显示,界面如下:
+ 财务部 ...
张三 ...
李四 ...
+ 行政部 ...
王二 ...
麻子 ...问题:
该DataGrid如何绑定 部门、员工两个表?看了msdn,只说了效果,没有例子,看不懂。参考:
msdn:Windows 窗体 DataGrid 控件介绍
如果 DataGrid 绑定到具有多个相关表的数据,并且在网格上启用了导航功能,则网格将在每行中都显示展开器。展开器允许从父表移动到子表。单击某个节点会显示子表,而单击“后退”按钮会显示原始的父表。在这种方式下,网格会显示各表间的分层关系。可以使用:DataGrid 控件中的多个表 在msdn中查询
vs2003
WinForm有2个表:部门,员工,要在1个DataGrid中显示,界面如下:
+ 财务部 ...
张三 ...
李四 ...
+ 行政部 ...
王二 ...
麻子 ...问题:
该DataGrid如何绑定 部门、员工两个表?看了msdn,只说了效果,没有例子,看不懂。参考:
msdn:Windows 窗体 DataGrid 控件介绍
如果 DataGrid 绑定到具有多个相关表的数据,并且在网格上启用了导航功能,则网格将在每行中都显示展开器。展开器允许从父表移动到子表。单击某个节点会显示子表,而单击“后退”按钮会显示原始的父表。在这种方式下,网格会显示各表间的分层关系。可以使用:DataGrid 控件中的多个表 在msdn中查询
http://dotnet.aspx.cc/ShowDetail.aspx?id=149E5DD7-3B32-461e-ACC6-51D1652E6746
http://www.syncfusion.com/faq/windowsforms/Search/780.aspx
this.dataAdapterDetails.Fill(this.dataSet, "Orders");
#region 定义变量
#region depart
DataTable dtDepart = new DataTable();
dtDepart.Columns.Add("ID",typeof(Int32));
dtDepart.Columns.Add("名称",typeof(String));
dtDepart.Rows.Add( new object[] { 1,"财务部" } );
dtDepart.Rows.Add( new object[] { 2,"行政部" } );
#endregion
#region user
DataTable dtUser = new DataTable();
dtUser.Columns.Add("ID",typeof(Int32));
dtUser.Columns.Add("部门",typeof(Int32));
dtUser.Columns.Add("名称",typeof(String));
dtUser.Rows.Add( new object[] { 1,1,"张三" } );
dtUser.Rows.Add( new object[] { 2,1,"李四" } );
dtUser.Rows.Add( new object[] { 3,2,"王二" } );
dtUser.Rows.Add( new object[] { 4,2,"麻子" } );
#endregion
#region 加入
DataSet dsTmp = new DataSet();
dtDepart.TableName = "部门";
dsTmp.Tables.Add( dtDepart );
dtUser.TableName = "员工";
dsTmp.Tables.Add( dtUser );
#endregion
#region 建立关系
DataColumn dcParent = dsTmp.Tables["部门"].Columns["ID"];;
DataColumn dcChild = dsTmp.Tables["员工"].Columns["部门"];;
DataRelation drTmp = new DataRelation("员工", dcParent, dcChild);
dsTmp.Relations.Add( drTmp );
#endregion
#endregion
#region 绑定
dgTmp.DataSource = dsTmp;
dgTmp.DataMember = "部门";
#endregion
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace test
{
public class Form1 : System.Windows.Forms.Form
{
#region vs
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.DataGrid dgTmp;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnTest = new System.Windows.Forms.Button();
this.dgTmp = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dgTmp)).BeginInit();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnTest.Location = new System.Drawing.Point(104, 58);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(75, 26);
this.btnTest.TabIndex = 6;
this.btnTest.Text = "测试";
this.btnTest.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
//
// dgTmp
//
this.dgTmp.DataMember = "";
this.dgTmp.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgTmp.Location = new System.Drawing.Point(104, 106);
this.dgTmp.Name = "dgTmp";
this.dgTmp.ReadOnly = true;
this.dgTmp.Size = new System.Drawing.Size(336, 256);
this.dgTmp.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(544, 421);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.dgTmp);
this.Name = "Form1";
this.Text = "试验DataGrid绑定多表";
((System.ComponentModel.ISupportInitialize)(this.dgTmp)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} #endregion
private void btnTest_Click(object sender, System.EventArgs e)
{
#region 定义变量
#region depart
DataTable dtDepart = new DataTable();
dtDepart.Columns.Add("ID",typeof(Int32));
dtDepart.Columns.Add("名称",typeof(String));
dtDepart.Rows.Add( new object[] { 1,"财务部" } );
dtDepart.Rows.Add( new object[] { 2,"行政部" } );
#endregion
#region user
DataTable dtUser = new DataTable();
dtUser.Columns.Add("ID",typeof(Int32));
dtUser.Columns.Add("部门",typeof(Int32));
dtUser.Columns.Add("名称",typeof(String));
dtUser.Rows.Add( new object[] { 1,1,"张三" } );
dtUser.Rows.Add( new object[] { 2,1,"李四" } );
dtUser.Rows.Add( new object[] { 3,2,"王二" } );
dtUser.Rows.Add( new object[] { 4,2,"麻子" } );
#endregion
#region 加入
DataSet dsTmp = new DataSet();
dtDepart.TableName = "部门";
dsTmp.Tables.Add( dtDepart );
dtUser.TableName = "员工";
dsTmp.Tables.Add( dtUser );
#endregion
#region 建立关系
DataColumn dcParent = dsTmp.Tables["部门"].Columns["ID"];;
DataColumn dcChild = dsTmp.Tables["员工"].Columns["部门"];;
DataRelation drTmp = new DataRelation("员工", dcParent, dcChild);
dsTmp.Relations.Add( drTmp );
#endregion
#endregion
#region 绑定
dgTmp.DataSource = dsTmp;
dgTmp.DataMember = "部门";
#endregion
}
}
}
http://blog.csdn.net/zhzuo/archive/2004/08/06/67016.aspx
我目前是做一个统计视图,所以需要1个DataGrid。
例如:
+ 按部门统计
财务部 ...
行政部 ...
+ 按级别统计
工程师 ...
高级工程师 ...