在form1里有一个按钮和一个datagridview,我把datagridview绑定到数据库里的T1表。现在我想点form1的按钮的时候弹出一个窗口form2,form2有很多textbox接受用户输入的数据,然后把这些数据作为T1的一条记录,返回到数据库的T1里,并且在form1里datagridview里显示新增出来这一条记录。该怎么办?
如果把form2里的textbox不放到form2中,放到form1中,就很容易实现,上述功能,因为他们在同一个窗口中。现在出现两个窗口怎么传递数据呢?

解决方案 »

  1.   

    在Form2里面提供一个公开函数,当form2关闭的时候,调用该方法把数据取回即可
      

  2.   

    第一种 委托,这个问题我已经回答不下3次了,论坛里面也总结过,我粗略的写一个。
        public Delegate void  Reload(string content);
         public class A:form 主窗体
          {
            ...........
            B b=new B();
            b.ReloadEvent+=(sender)=>{this.textbox1.text=sender as string;};
           }     public class B:form  弹出窗体 
          {
              public event Reload ReloadEvent;
              ..................
              button.click +=(sender,args)=>
    {
       if(ReloadEvent!=null)
           ReloadEvent(textbox2.text.trim());// 这里就代表该事件执行了。
    };
           }第二个,构造函数;
             public class A:form 主窗体
          {
            private string content;
            B b=new B(content);
            if(b.showdilog()==dilogresult.ok)
              {
    this.textbox1.text=b.Content;
         } 
                 
     }     public class B:form  弹出窗体 
          {
              private string  content;
              public string Content(get return content;)
              public B(string _content)
               {content=_content;}
              ..................
            btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            B.FormClosing +=(sender,args)=>
    {         
    if (DialogResult == DialogResult.OK)
               content=textbox2.text.trim();};
           }