form1 中有textbox1,在textbox1回车后,弹出form2,双击form2中的dataGridView1中的一列,显示某一列的值在form1 的textbox1中。并关闭form2,求代码;dataGridView1中的值为:         String sql2 = "select [序号],[医院名称],[服务项目],[报销类别],[申请时间],[报销时间],[项目金额],[报销标志],IIF([报销标志]='1','已报销','未报销') as 报销状态 from memoA order by [序号] desc  ";            //String sql2 = "select [userid] from userB";            DataSet ds = new DataSet();            ds = Cdatabase.GetDataFromDB(sql2);            this.dataGridView1.DataSource = ds.Tables[0]; 请问如何实现,最好是有form1和form2的实现代码 

解决方案 »

  1.   

    http://www.cnblogs.com/cosoft/archive/2011/08/08/2130659.html
      

  2.   


     public class Form1 : Form
        {
            private TextBox txtBox;        public Form1()
            {
                txtBox = new TextBox();
                txtBox.Location = new Point(0, 0);
                txtBox.KeyPress += new KeyPressEventHandler(txtBox_KeyPress);
            }        void txtBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == 13)
                {
                    Form2 frm2 = new Form2();
                    if (frm2.ShowDialog() == DialogResult.OK)
                    {
                        txtBox.Text = frm2.ReturnValue;
                    }
                }
            }
        }    public class Form2 : Form
        {        public string ReturnValue
            {
                get;
                set;
            }        public Form2()
            {
              
            }        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                ReturnValue = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }       
        }
      

  3.   

      public string Txt
            {
                get { return textBox1.Text; }
                set { textBox1.Text = value; }
            }
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    Form2 f = new Form2();
                    f.ShowDialog(this);
                }
            } 
     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                Form1 f = (Form1)this.Owner;
                f.Txt = this.dataGridView1.Rows[e.RowIndex].Cells[你的列名].Value.ToString();
                this.Close();
                        }
            
        }
      

  4.   

    要激活keydown事件需设置form1的属性 KeyPreview 为 true
      

  5.   

    可以在form2中dataGridView1中的一列是否被选中获取鼠标的点击事件发出的信息, 如果是双击就调用一个自定义的方法把你想要的值赋值到相应的位置。
      

  6.   

    如果Frmmain是父窗体,form1和子窗体,然后弹出form2,这时候 下面这句话:
    Form1 f = (Form1)this.Owner;          --- 
     f.Txt = this.dataGridView1.Rows[e.RowIndex].Cells[你的列名].Value.ToString();    
    第一句话就会报“无法将类型为“.Frmmain”的对象强制转换为类型“.Form1”。”     
    这样如何解决呢?