windform如何选中datagrid的行后打开新窗体,在新窗体修改保存数据后刷新回页面中?
如题,不是主从窗体. 问题点数:50、回复次数:15Top
1 楼liaodanz(雪宇)回复于 2005-04-01 18:21:48 得分 0
主要是怎么双击datagrid中其中一行,确定数据到新窗体里?用变量传递id?大家给个思路啊.Top
2 楼superhasty(鸟儿自空中飞过)回复于 2005-04-01 22:21:17 得分 5
获得dataGrid的当前选中行号和数据:
BindingManagerBase bmb=this.BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
//MessageBox.Show(bmb.Count.ToString()); //获取当前dataGrid中的实际行数
//MessageBox.Show(bmb.Position.ToString()); //获取当前选择的行
MessageBox.Show(dataGrid1[bmb.Position][0]);//当前行的第一列数据
Form2 sForm=new Form2(dataGrid[bmb.Position][0]); //注:Form2有一个重载的构造函数
sForm.Show();Top
3 楼liuxiang027(Nail.柳翔)回复于 2005-04-04 09:00:19 得分 0
广告``广告时间`````````休息休息
群号码:9978078
群名称:vs.net开发—C#篇
欢迎各位菜鸟和大虾能够帮忙来顶`````````希望能够在此多多交流共同学习!Top
4 楼liuxiang027(Nail.柳翔)回复于 2005-04-04 09:05:03 得分 5
在来顶一下````````
--------------------------------------------------------------------------
BindingManagerBase bmb=this.BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
//MessageBox.Show(bmb.Count.ToString()); //获取当前dataGrid中的实际行数
//MessageBox.Show(bmb.Position.ToString()); //获取当前选择的行
MessageBox.Show(dataGrid1[bmb.Position][0]);//当前行的第一列数据
Form2 sForm=new Form2(dataGrid[bmb.Position][0]); //注:Form2有一个重载的构造函数
sForm.Show();
--------------------------------------------------------------------------
Form2 sForm=new Form2(dataGrid[bmb.Position][0]);
sForm.Show();
这2句话```就算数据是从FORM1窗体传来的```Form2接收````但是怎么值 显示呢???
我是菜鸟``希望楼上大虾指教````谢谢```sForm.Show();这个只是显示FORM2窗口```
而且``这样的话``FORM1的窗口不能随着FORM2的显示而关闭啊```(抱拳)```请多多指教Top
5 楼zhzuo(秋枫)回复于 2005-04-04 13:30:03 得分 40
//在另一个窗口中修改当前选定的DataGrid的行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//主窗体代码
namespace Zhzuo
{
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;
this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
//
// 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();
}
private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
button1_Click(button1,EventArgs.Empty);
}
}
}
//子窗体代码
=======================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Zhzuo
{
/// <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();
}
}
}
Top
6 楼liaodanz(雪宇)回复于 2005-04-07 12:04:10 得分 0
谢谢秋枫的源代码。。不过我照那个改编了下。但是修改后,他的数据库并没有进行操作。又该怎么做啊???而且不知道为什么da.update(ds,"note")不能用~~~?Top
7 楼tl_pear(飘叶寻梦)回复于 2005-04-07 13:09:40 得分 0
你的意思就是在双击datagrid中其中一行时,把你双击行的值传给另一个窗体?
在双击的时候,你应该会怎么样去取得值吧!
那么你就在另一窗体里建立相应的变量和属性,通过属性把值传过去。
下面的例子就是,在点击form1的button1时把form1里textbox1.text的值传给form2。
首先在form2里创建一个相应的变量和属性如:
private string str1;
public string getstr
{
get
{
return str1;
}
set
{
if (value != str1)
{
str1 = value;
}
}
}
在form2里的button1_click事件里测试str1的值;
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(str1);
}
在form1里的button1_click事件如下:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 obj =new Form2();
obj.getstr=textBox1.Text ;
obj.Show();
}
Top
8 楼tl_pear(飘叶寻梦)回复于 2005-04-07 13:18:53 得分 0
双击datagrid1的事件:
取得值1,值2,值3……
声明一个窗体实例变量。
变量的属性1=值1
变量的属性2=值2
变量的属性3=值3
显示窗体
在新窗体中:
声明:变量1,对应的属性1;
变量2,对应的属性2;
变量3,对应的属性3;
按这个步聚就可以了!Top
9 楼thundersoft(神州春雷)回复于 2005-04-07 14:57:44 得分 0
MarkTop
10 楼liaodanz(雪宇)回复于 2005-04-08 20:28:39 得分 0
致飘叶寻梦~~!谢谢~现在这个问题我遇到的问题是。新窗体修改数据后又返回原窗体的datagrid1并要更新到数据库。datagrid1的数据更改了,怎么到数据库的操作?``~Top
11 楼xiaomatian(趴趴熊◎%#……※×)回复于 2005-04-09 13:51:42 得分 0
我一般是在新窗体修改后就立即更新到数据库,更新好了之后就刷新datagrid数据(重新进行绑定)Top
12 楼liaodanz(雪宇)回复于 2005-04-10 17:46:35 得分 0
怎么在新窗体执行另一个窗体控件的datagrid的数据重新绑定操作?Top
13 楼lvwaike(螃蟹)回复于 2005-04-11 11:03:40 得分 0
怎么在新窗体执行另一个窗体控件的datagrid的数据重新绑定操作?
Top
14 楼mopeboy()回复于 2005-04-11 12:16:25 得分 0
怎么在新窗体执行另一个窗体控件的datagrid的数据重新绑定操作?Top
15 楼down12345(努力学习天天向上)回复于 2005-07-04 23:34:35 得分 0
顶..Top




