日期:2014-05-18  浏览次数:20691 次

winform1和winform2一起打开时,点击winform2里的按钮刷新winform1的datagridview
如题,请教一个问题。
winform3和winform4一起打开时,点击winform4里的按钮刷新winform3的datagridview。
C# code

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "" && this.textBox4.Text != "")
            {
                try
                {
                    conn = new SqlConnection("server=.;database=pangoo;uid=sa;pwd=drinker;Asynchronous Processing=True;");
                    conn.Open();
                    string cmdInsert = string.Empty;
                    cmdInsert = "insert into users(userid,username,department,password,create_time) ";
                    cmdInsert += " values('" + this.textBox1.Text + "'";
                    cmdInsert += ",'" + this.textBox2.Text + "'";
                    cmdInsert += ",'" + this.textBox3.Text + "'";
                    cmdInsert += ",'" + this.textBox4.Text + "'";
                    cmdInsert += ",getdate())";
                    SqlCommand cminsert = new SqlCommand(cmdInsert, conn);
                    cminsert.ExecuteNonQuery();
                    this.Hide();
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            else
            { MessageBox.Show("不允许存在空格!"); }

        //form3里面的button1事件实现刷新winform4的datagridview。
          //如今存在问题是,点击button1刷新不了已打开的那个form3中的datagridview。
            Form3 fm3 = new Form3();
            fm3.button1_Click(null, null);
            


        }





求救!!

------解决方案--------------------
你打开的那个form不是你现在的 Form3 fm3 = new Form3();
你要去理解什么是实例。
------解决方案--------------------
http://topic.csdn.net/u/20110407/19/c1068d69-7331-4d02-bc0b-f5ba7a5f8dd8.html
------解决方案--------------------
C# code

            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "Form3")
                {
                    //把更新的数据导入到datatable中传给Form3
                    frm.Update(datatable);
                    frm.Show();
                    break;
                }
            }

------解决方案--------------------
看这个吧,http://www.cnblogs.com/ysh2012/archive/2012/07/28/2612807.html