1.公用一个DATASET似乎有点问题 (一式双份可能还可以)
2.?
3.脱离DATAGRID另外写
4.随便你实现咯
2.?
3.脱离DATAGRID另外写
4.随便你实现咯
解决方案 »
- 路径不对,图片显示不出来的问题
- [续]双击打开exe程序和System.Diagnostics.Process.Start(@"c:\xxx.exe")的区别问题发现:win7权限问题
- C# 调用flash控件,发现不能输入中文?
- 新手请教:ComboBox控件定制的问题【50分】
- 用c#.net写的程序可以在vista下运行吗?
- C#怎么获得拍摄照片的数码相机的信息?
- 关于asp.net2.0的TreeView问题
- 程序发布成Application Reference文件后找不到了
- C#开发过海康威视的进 急急急
- 存储过程参数传递报错!!
- 请各位兄长帮忙解决,非常紧急,谢谢
- 对 C# 和 JavaScript 的 正则 的疑问:
1.可以重写form1的构造函数,将mainform的指针传过去(前提是dateset是mainform的类成员)这样可以共用了。
2.首先你要将在mainform选中行号记录下来,form1中开个计数器,邦定它,通过按钮事件驱动对改变就可以了。
3.新增数据很简单啊,System.Data.SqlClient.SqlDataAdapter.insertcommand写好。
4.实在没理解~~~~!(我没遇到过这样的问题)
我们现在的做法是,全部在Form1中进行新增、编辑,但只是针对一条记录,所以
1、在新增的时候,我一般会在FrmMain中先新增一条记录,然后传给Form1,在那里面修改(通过界面绑定),提交后在FrmMain中进行数据保存
2、在修改的时候,就直接把选中的记录(一个DataRow)传给Form1
不过我们系统的风格是不在Form1中进行新增和多行的操作
首先取得FrmMain的所选记录的PK,在Form1得到dataset后,根据PK值查找出其在table的第几行(index)。然后this.BindContext[this.dataset , "tablename"].Position = index;(2)在FrmMain中添加事件(当Form1中记录指针变化时引发)处理,此方法适用于show()显示的Form1
如果是ShowDialog()的话,可以用(1)里面的方法反过来用。(3)因为是绑定的,直接NewRow,再将当前行定位到新增的行上就可以了。(4)会。(可以多参考BindContext的用法或是参看微软的数据窗体的代码,很值得学习的)
2、这样就简单了,用ShowDialog显示FORM之前,先dim myfrm as new form1
myfrm.textbox1 = ds.tables(0).row(0).item(0).tostring3、改完后,可以ds.tables(0).row(0).item(0) = myfrm.textbox1
{
Form1 frm=new Form1(this);
frm.show;
}
在Form1 添加public 对象 public MainForm frm;
添加From1的构造方法
Form1(MainForm frm)
{
this.frm=frm;
}
然后两者公用一个dataset就可以了
要在MainForm的激活事件中 添加dataGrid的刷新
(家里没有C#可能有些地方有错 海涵!!^-^ 但就是这个意思)
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//主窗体代码
namespace ZZ
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet ds;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
ds = new DataSet("MyDataSet");
InitData(ds);
this.dataGrid1.DataSource = this.ds;
this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
} /// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 20);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(340, 160);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(264, 196);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "修改";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(372, 229);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion private void InitData(DataSet ds)
{
DataTable dt = new DataTable("TabeHost");
DataColumn dc = new DataColumn("IpAddress",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UserName",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Password",typeof(string));
dt.Columns.Add(dc);
ds.Tables.Add(dt);
DataRow dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.229";
dr["UserName"] = "zhzuo";
dr["Password"] = "zhengzuo";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.230";
dr["UserName"] = "11";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.231";
dr["UserName"] = "123";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.232";
dr["UserName"] = "22";
dr["Password"] = "788";
dt.Rows.Add(dr);
} private void button1_Click(object sender, System.EventArgs e)
{
DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
Form2 form2 = new Form2(drv);
form2.ShowDialog(); }
}
}
=======================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ZZ
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private DataRowView drv;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2(DataRowView dr)
{
InitializeComponent();
this.drv = dr;
this.textBox1.Text = (string)drv["IpAddress"];
this.textBox2.Text = (string)drv["UserName"];
this.textBox3.Text = (string)drv["Password"];
} /// <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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(28, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(28, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(28, 92);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "textBox3";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(236, 152);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(324, 201);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
drv["IpAddress"] = this.textBox1.Text;
drv["UserName"] = this.textBox2.Text;
drv["Password"] = this.textBox3.Text;
} private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}