想实现的场景:点击form1里的按钮,弹出form2,填完数据后点确定,form1里的DataGridView同步更新,数据库也更新我的方法:自定义一个填充DataSet的方法,点击form2的确定按钮时调用此方法,并实例化form1,然后绑定DataGridView的数据源问题:datagridview没反应代码如下://自定义类wangyy 里的两个方法public static SqlConnection con()
{//连接数据库
MyConn = new SqlConnection(strconn);
MyConn.Open();
return MyConn;
}
public static DataSet getDataSet(string SQLstr, string tableName)
{ 填充DataSet对象
con();
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr ,MyConn);
DataSet MyDs = new DataSet();
SQLda.Fill(MyDs,tableName);
MyConn.Close();
return MyDs;
}
//form1的添加按钮private void button1_Click(object sender, EventArgs e)
{
//添加
Form4 form4 = new Form4();
form4.Visible = true;
}
//form2的确定按钮private void button1_Click(object sender, EventArgs e)
{
try
{
wangyy.con();
cmd = new SqlCommand("insert into userinfo values('" + textBox1.Text + "','" + textBox2.Text + "')", wangyy.MyConn);
cmd.ExecuteNonQuery();
wangyy.MyConn.Close();
DataSet ds = wangyy.getDataSet("select * from userinfo", "tianchong");
Form3 fm3 = new Form3();
fm3.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("添加数据成功!");
}
catch
{ MessageBox.Show("添加数据出错!"); }
}我的分不多啊,诸位请帮帮忙
{//连接数据库
MyConn = new SqlConnection(strconn);
MyConn.Open();
return MyConn;
}
public static DataSet getDataSet(string SQLstr, string tableName)
{ 填充DataSet对象
con();
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr ,MyConn);
DataSet MyDs = new DataSet();
SQLda.Fill(MyDs,tableName);
MyConn.Close();
return MyDs;
}
//form1的添加按钮private void button1_Click(object sender, EventArgs e)
{
//添加
Form4 form4 = new Form4();
form4.Visible = true;
}
//form2的确定按钮private void button1_Click(object sender, EventArgs e)
{
try
{
wangyy.con();
cmd = new SqlCommand("insert into userinfo values('" + textBox1.Text + "','" + textBox2.Text + "')", wangyy.MyConn);
cmd.ExecuteNonQuery();
wangyy.MyConn.Close();
DataSet ds = wangyy.getDataSet("select * from userinfo", "tianchong");
Form3 fm3 = new Form3();
fm3.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("添加数据成功!");
}
catch
{ MessageBox.Show("添加数据出错!"); }
}我的分不多啊,诸位请帮帮忙
解决方案 »
- COM组件问题
- 怎样在Combox下拉列表中添加ChcekBox复选框
- 就没有人知道怎么控制UpdatePanel里的textbox滚动条到最低端吗?希望不吝赐教!
- 找不到类型或命名空间名称“boolean”(是否缺少 using 指令或程序集引用?)
- 问几个socket编程的问题~~~~
- RasDial,这个函数具体怎么用,调用时参数怎么么设置,如果只设entryname这个参数,能不能调用成功?
- 如何判断一个类是另一个类的子类?
- 我想在datagrid当然的row插人一条row怎么写???
- 中秋节 大送分
- 请问各位大哥怎么将int类型的数据转换为byte[]
- 如何实现在记事本的查找功能
- 如何让DevExpress的DateEdit控件中显示中文按钮
至于两个窗体的数据同步问题,你说form1里有个DataGridView,那在DataGridView与数据源之间加个DataBindingSource控件,并在显示第二个窗体的时间将其作为参数传过去,这是MSDN上说的,也可以按你自己的想法自行实现同步!
{
//添加
//Form4 form4 = new Form4();
//form4.Visible = true;
Form2 frm=new Form2();
frm.Owner=this;
frm.show();
}
//form2的确定按钮private void button1_Click(object sender, EventArgs e)
{
try
{
wangyy.con();
cmd = new SqlCommand("insert into userinfo values('" + textBox1.Text + "','" + textBox2.Text + "')", wangyy.MyConn);
cmd.ExecuteNonQuery();
wangyy.MyConn.Close();
DataSet ds = wangyy.getDataSet("select * from userinfo", "tianchong");
Form1 frm=(Form1)Owner;
frm.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("添加数据成功!");
}
catch
{ MessageBox.Show("添加数据出错!"); }
}
2、在from2中放一个from1中委托类型属性
3、在from2 button1_Click事件中调用委托
我试下
我再想想其它方法去
另外,这个怎么给你们分啊?(我第一次发帖,不会 - -)