当前位置:编程学习 > C#/ASP.NET >>

A 如何得到B中双击的值

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的实现代码  --------------------编程问答-------------------- http://www.cnblogs.com/cosoft/archive/2011/08/08/2130659.html --------------------编程问答-------------------- http://bbs.csdn.net/topics/360140208 --------------------编程问答--------------------

 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();
        }

       
    }
--------------------编程问答--------------------
  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();
            

        }
        
    }
--------------------编程问答-------------------- 要激活keydown事件需设置form1的属性 KeyPreview 为 true --------------------编程问答-------------------- 不行啊,双击之后没有任何反应 --------------------编程问答-------------------- lenovore 方式可用,谢谢 --------------------编程问答-------------------- 可以在form2中dataGridView1中的一列是否被选中获取鼠标的点击事件发出的信息, 如果是双击就调用一个自定义的方法把你想要的值赋值到相应的位置。 --------------------编程问答-------------------- 如果Frmmain是父窗体,form1和子窗体,然后弹出form2,这时候 下面这句话:
Form1 f = (Form1)this.Owner;          --- 
 f.Txt = this.dataGridView1.Rows[e.RowIndex].Cells[你的列名].Value.ToString();    
第一句话就会报“无法将类型为“.Frmmain”的对象强制转换为类型“.Form1”。”     
这样如何解决呢?     --------------------编程问答-------------------- 使用委托 六字 --------------------编程问答-------------------- 如何更改?
 
 
引用
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();
            

        }
        
    }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,