有两个窗体,第一个窗体用DataGridView显示数据和按钮(弹出窗体2)。在窗体2中有两个textbox和一个添加按钮,当点击添加按钮时,怎么在第一个窗体的datagridview显示添加的内容并关闭窗体2。

解决方案 »

  1.   

    委托
    http://topic.csdn.net/u/20100106/20/e9697297-75f9-4450-aaa9-da0da416cf41.html?39668
      

  2.   

    一种方法可以在按钮(弹出窗体2)的代码“窗体2.showdialog()”代码后面直接写提取代码来显示添加的内容,这种方法需要在窗体2中定义属性来表示。
    第二种方法可以在点击添加按钮后利用窗体的find()函数来找到窗体1中的控件来设置其显示内容以显示新添加的数据。
    希望对你有帮助哦!!!!!!! 呵呵
      

  3.   

       private void btnSave_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pubs;User ID=sa;Password=sa");
                conn.Open();
                
                string strlname=tbLname.Text;
                string strfname=tbFname.Text;
                string strphone=tbPhone.Text;
                string strcity=tbCity.Text;
                string straddress=tbAddress.Text;            string strCmd = "update authors set au_lname='" + strlname + "',au_fname='" + strfname +
                    "',phone='" + strphone + "',city='" + strcity + "',address='" + straddress + "' where au_id='"+this.Au_id+"'";
                SqlCommand comm = new SqlCommand(strCmd, conn);
                int count=comm.ExecuteNonQuery();
                if (count > 0)
                    MessageBox.Show("修改成功!");
                else
                {
                    MessageBox.Show("修改失败!");
                    return;
                }            conn.Close();            Control[] control= _form.Controls.Find("datagridview1", false);
                //SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pubs;User ID=sa;Password=sa");
                conn.Open();
                string strSql = "select * from authors ";
                SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "authors");
                DataGridView dgv = (DataGridView)control[0];
                dgv.DataSource = ds.Tables[0];            conn.Close();            this.Close();
            }
      

  4.   

    弹出窗体2事件
    form2 f2=new form2();
    f2.owner=this;
    f2.show();
    窗体1中定义一个公有的方法
    public void DataBind(datatale dt)
    {
      this.datagridview.datasource=dt;
    }
    窗体2按钮事件
    添加后查询该结果到datatable中
    form1 f1=new form1();
    form2 f2=this.ower as form2;
    f2.databind(dt);
    思路就这样的
      

  5.   

    好方法是委托,上面都有,我就不说了。还有一个笨方法,就是在form2中先将数据添加到数据库,然后关闭form2,回到form1时重新读取数据,刷新dvg。