datagridview中如何实现父子表??? 想实现父子表显示,像datagrid中的+号显示与隐藏 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个功能是DataGridView所缺少的!我也在找这个实现方法,找到后 ,通知兄弟一声,谢谢啦! 使用DataRelation//同时能更新主表和子表using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;namespace WindowsApplication3{/// /// Form1 的摘要说明。/// public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.Button button1;private System.Windows.Forms.DataGrid dataGrid1;private System.Windows.Forms.Splitter splitter1;private System.Windows.Forms.Button button2;private System.Windows.Forms.Panel panel1;private System.Windows.Forms.Button button3;private SqlDataAdapter myDataAdapter1,myDataAdapter2;private DataSet ds;/// /// 必需的设计器变量。/// private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// /// 清理所有正在使用的资源。/// protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// private void InitializeComponent(){this.button1 = new System.Windows.Forms.Button();this.dataGrid1 = new System.Windows.Forms.DataGrid();this.splitter1 = new System.Windows.Forms.Splitter();this.button2 = new System.Windows.Forms.Button();this.panel1 = new System.Windows.Forms.Panel();this.button3 = new System.Windows.Forms.Button();((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();this.panel1.SuspendLayout();this.SuspendLayout();// // button1// this.button1.Location = new System.Drawing.Point(344, 0);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(104, 23);this.button1.TabIndex = 0;this.button1.Text = "显示用户库数据";this.button1.Click += new System.EventHandler(this.button1_Click);// // dataGrid1// this.dataGrid1.AlternatingBackColor = System.Drawing.SystemColors.Window;this.dataGrid1.BackColor = System.Drawing.SystemColors.Info;this.dataGrid1.DataMember = "";this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;this.dataGrid1.Location = new System.Drawing.Point(0, 0);this.dataGrid1.Name = "dataGrid1";this.dataGrid1.Size = new System.Drawing.Size(464, 318);this.dataGrid1.TabIndex = 1;// // splitter1// this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;this.splitter1.Location = new System.Drawing.Point(0, 0);this.splitter1.Name = "splitter1";this.splitter1.Size = new System.Drawing.Size(464, 3);this.splitter1.TabIndex = 2;this.splitter1.TabStop = false;// // button2// this.button2.Location = new System.Drawing.Point(200, 0);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(96, 23);this.button2.TabIndex = 3;this.button2.Text = "保存到消息库";this.button2.Click += new System.EventHandler(this.button2_Click);// // panel1// this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;this.panel1.Controls.Add(this.button3);this.panel1.Controls.Add(this.button1);this.panel1.Controls.Add(this.button2);this.panel1.Dock = System.Windows.Forms.DockStyle.Top;this.panel1.Location = new System.Drawing.Point(0, 3);this.panel1.Name = "panel1";this.panel1.Size = new System.Drawing.Size(464, 21);this.panel1.TabIndex = 4;// // button3// this.button3.Location = new System.Drawing.Point(64, 0);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(88, 23);this.button3.TabIndex = 4;this.button3.Text = "保存到用户库";this.button3.Click += new System.EventHandler(this.button3_Click);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(464, 318);this.Controls.Add(this.panel1);this.Controls.Add(this.splitter1);this.Controls.Add(this.dataGrid1);this.Name = "Form1";this.Text = "Form1";((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();this.panel1.ResumeLayout(false);this.ResumeLayout(false);}#endregion/// /// 应用程序的主入口点。/// [STAThread]static void Main() {Application.Run(new Form1());}//按钮事件private void button1_Click(object sender, System.EventArgs e){//定义数据库连接字符string cnStr="Password=123456;Persist Security Info=False;User ID=sa;Initial Catalog=DBOA;Data Source=YORK";//创建连接对象SqlConnection cn=new SqlConnection(cnStr);//定义SQL语句string sqlStr="select * from t_cardcontent1";//创建适配器对象myDataAdapter1 = new SqlDataAdapter(sqlStr,cn);//创建数据集对象ds = new DataSet();//将t_cardcontent1表填充到数据集myDataAdapter1.Fill(ds,"t_cardcontent1");//查询t_cardcontent7表的SQL语句sqlStr="select * from t_cardcontent7";//t_cardcontent7的适配器myDataAdapter2 = new SqlDataAdapter(sqlStr,cn); myDataAdapter2.Fill(ds,"t_cardcontent7");//一次只能将一个SqlDataAdapter与一个SqlCommandBuilder对象互相关联SqlCommandBuilder cb1=new SqlCommandBuilder(myDataAdapter1);SqlCommandBuilder cb2=new SqlCommandBuilder(myDataAdapter2);//构造数据关系对象,两个表之间的关系DataRelation dr=new DataRelation("显示对应的消息库数据",ds.Tables["t_cardcontent1"].Columns["field1"],ds.Tables["t_cardcontent7"].Columns["owner"]);//添加数据关系到数据集ds.Relations.Add(dr);//将数据集中的表t_cardcontent1绑定到DataGrid控件dataGrid1.SetDataBinding(ds,"t_cardcontent1"); }//更新不同的表需要不同的适配器private void button2_Click(object sender, System.EventArgs e){myDataAdapter2.Update(ds,"t_cardcontent7");//用已修改的数据集更新数据源MessageBox.Show("成功保存到消息库!");}private void button3_Click(object sender, System.EventArgs e){myDataAdapter1.Update(ds,"t_cardcontent1");MessageBox.Show("成功保存到用户库!");}}} 请楼主看此贴,就会得到答案:http://community.csdn.net/Expert/topic/4982/4982848.xml?temp=.4859888 http://community.csdn.net/Expert/topic/4983/4983948.xml?temp=.7148706 http://community.csdn.net/Expert/topic/4982/4982848.xml?temp=.1613275 怎样将WinForm的某个控件打包成dll? 如何在非web中,使用HtmlEncode C#操作Excel 问个sql查询语句 c#,.net,sqlserver,物流系统,物流软件 求中级C#电子书的下载地址 怎么样让别人通过我IP地址进入我的网站 关于多线程共享资源的问题 在datagridview里输入一条及多条,然后插入到数据库中, 如何实现串口通迅用C# 撒分拉,学c#用什么书最好啊 c#怎么会内存泄露??????
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace WindowsApplication3
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button3;
private SqlDataAdapter myDataAdapter1,myDataAdapter2;
private DataSet ds;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.splitter1 = new System.Windows.Forms.Splitter();
this.button2 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "显示用户库数据";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.AlternatingBackColor = System.Drawing.SystemColors.Window;
this.dataGrid1.BackColor = System.Drawing.SystemColors.Info;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 318);
this.dataGrid1.TabIndex = 1;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(464, 3);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(200, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 23);
this.button2.TabIndex = 3;
this.button2.Text = "保存到消息库";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.button2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 21);
this.panel1.TabIndex = 4;
//
// button3
//
this.button3.Location = new System.Drawing.Point(64, 0);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 23);
this.button3.TabIndex = 4;
this.button3.Text = "保存到用户库";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 318);
this.Controls.Add(this.panel1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);}
#endregion///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}//按钮事件
private void button1_Click(object sender, System.EventArgs e)
{
//定义数据库连接字符
string cnStr="Password=123456;Persist Security Info=False;User ID=sa;Initial Catalog=DBOA;Data Source=YORK";
//创建连接对象
SqlConnection cn=new SqlConnection(cnStr);
//定义SQL语句
string sqlStr="select * from t_cardcontent1";
//创建适配器对象
myDataAdapter1 = new SqlDataAdapter(sqlStr,cn);
//创建数据集对象
ds = new DataSet();
//将t_cardcontent1表填充到数据集
myDataAdapter1.Fill(ds,"t_cardcontent1");//查询t_cardcontent7表的SQL语句
sqlStr="select * from t_cardcontent7";
//t_cardcontent7的适配器
myDataAdapter2 = new SqlDataAdapter(sqlStr,cn);
myDataAdapter2.Fill(ds,"t_cardcontent7");
//一次只能将一个SqlDataAdapter与一个SqlCommandBuilder对象互相关联
SqlCommandBuilder cb1=new SqlCommandBuilder(myDataAdapter1);
SqlCommandBuilder cb2=new SqlCommandBuilder(myDataAdapter2);//构造数据关系对象,两个表之间的关系
DataRelation dr=new DataRelation("显示对应的消息库数据",ds.Tables["t_cardcontent1"].Columns["field1"],ds.Tables["t_cardcontent7"].Columns["owner"]);
//添加数据关系到数据集
ds.Relations.Add(dr);//将数据集中的表t_cardcontent1绑定到DataGrid控件
dataGrid1.SetDataBinding(ds,"t_cardcontent1"); }//更新不同的表需要不同的适配器
private void button2_Click(object sender, System.EventArgs e)
{
myDataAdapter2.Update(ds,"t_cardcontent7");//用已修改的数据集更新数据源
MessageBox.Show("成功保存到消息库!");
}private void button3_Click(object sender, System.EventArgs e)
{
myDataAdapter1.Update(ds,"t_cardcontent1");
MessageBox.Show("成功保存到用户库!");
}
}
}
http://community.csdn.net/Expert/topic/4982/4982848.xml?temp=.4859888