从Gridview获取数据到其他Windows窗体

时间:2014-04-09 07:11:25

标签: c# winforms gridview key enter

我有Windows窗体form1,按 Enter 键会打开一个新的form2。在form2中,有一个显示不同值的datagridview。现在,在form2中按 Enter 键时,我获得了datagridview的选定值。问题是我想将该值移至上一个form1但是当我尝试它时会打开一个新表单并移动该值。

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form37 f = new Form37();
        f.a = 1;
        f.v = dataGridView1.SelectedCells[0].Value.ToString();

        f.Show();
    }
}

2 个答案:

答案 0 :(得分:0)

您的第二个表单需要引用第一个表单:

<强> Form37:

编辑Form37 keydown事件

private void Form37_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form38 f = new Form38();
        f.caller_form = this;
        this.Hide();
        f.Show();
    }
}

<强> Form38:

Form38班级的根添加新变量:

public partial class Form38 : Form
    {
       Form37 caller_form;

修改Form38&#39; s dataGridView1的keydown事件

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Form37 f = caller_form;
        f.a = 1;
        f.v = dataGridView1.SelectedCells[0].Value.ToString();
        f.Show();
        this.Close();
    }
}

答案 1 :(得分:0)

        //  Mimize Your From1 using
          From1.WindowState = FormWindowState.Minimized;
         //..... 

        // Key press On data Grid
        //.....


        //using this get from37 and 
    foreach (Form  frms in this.MdiChildren)
{
    if (frms is from37 )
    {
        if (frms.WindowState==FormWindowState.Minimized)
        {
            frms.WindowState = FormWindowState.Maximized;

            //Minimized current form;
            this.WindowState = FormWindowState.Minimized;
        }
    }
}