Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 双击datagridview中的行,获取特定单元格的值并将其传递给另一个窗体_C#_Winforms_.net 4.0 - Fatal编程技术网

C# 双击datagridview中的行,获取特定单元格的值并将其传递给另一个窗体

C# 双击datagridview中的行,获取特定单元格的值并将其传递给另一个窗体,c#,winforms,.net-4.0,C#,Winforms,.net 4.0,我只是想知道下面的情况是否正确 我正在努力做到以下几点 1-从MyDataGridView1中的Form1中,在duoble上单击MyDataGridView1行,我获取特定列的值,并将其传递给FORM2 2-将该值分配给Form2 从datagridview private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { vID = MyDataGridV

我只是想知道下面的情况是否正确

我正在努力做到以下几点 1-从
MyDataGridView1
中的
Form1
中,在duoble上单击
MyDataGridView1
行,我获取特定列的值,并将其传递给
FORM2

2-将该值分配给
Form2

datagridview

private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
       vID = MyDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

       this.Close();
       Form2 NewForm = new Form2();
       NewForm.Show();
    }
然后将值保存到

public static string vID ;
无表格2

Textboxid.Text = F0103.vID ;
在下面的第行,我正在尝试使用
textbox
的请假事件,但它不起作用。是否有任何关于请假事件的想法

BTN_Refresh.Focus(); 

如果我正确理解你的问题。您可以将字符串传递给Form2构造函数

private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
  vID = MyDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

  Form2 NewForm = new Form2(vID);

  this.Close();

  NewForm.Show();
}
然后在构造函数中,将
文本框设置为该字符串

public class Form2
{
  public Form2(string vid)
  {
    InitializeComponent();

    Textboxid.Text = vid;
  }
}

如果我正确理解你的问题。您可以将字符串传递给Form2构造函数

private void MyDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
  vID = MyDataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

  Form2 NewForm = new Form2(vID);

  this.Close();

  NewForm.Show();
}
然后在构造函数中,将
文本框设置为该字符串

public class Form2
{
  public Form2(string vid)
  {
    InitializeComponent();

    Textboxid.Text = vid;
  }
}
完整答案:

表格1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        List<Stuff> stuff = new List<Stuff>();
        stuff.Add(new Stuff() { Foo = "Foo1", Bar = "Bar1", Data = "Data1" });
        stuff.Add(new Stuff() { Foo = "Foo2", Bar = "Bar2", Data = "Data2" });

        var bindingList = new BindingList<Stuff>(stuff);
        var source = new BindingSource(bindingList, null);
        dataGridView1.DataSource = source;
    }

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        string arg = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

        Form2 form2 = new Form2(arg);
        form2.Show();

        this.Hide();
    }
}

public class Stuff
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public string Data { get; set; }
}
winforms的一个特点(以及我为什么使用WPF)是它过于复杂了。根据我的测试,
this.Close()
关闭了整个应用程序,我不得不使用
this.Hide()

此外,养成传递参数而不是设置全局变量的习惯也是很好的。几乎C#中的任何内容都可以以某种方式作为参数传递,这有助于生成更好、更清晰的代码

完整的答案:

表格1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        List<Stuff> stuff = new List<Stuff>();
        stuff.Add(new Stuff() { Foo = "Foo1", Bar = "Bar1", Data = "Data1" });
        stuff.Add(new Stuff() { Foo = "Foo2", Bar = "Bar2", Data = "Data2" });

        var bindingList = new BindingList<Stuff>(stuff);
        var source = new BindingSource(bindingList, null);
        dataGridView1.DataSource = source;
    }

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        string arg = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

        Form2 form2 = new Form2(arg);
        form2.Show();

        this.Hide();
    }
}

public class Stuff
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public string Data { get; set; }
}
winforms的一个特点(以及我为什么使用WPF)是它过于复杂了。根据我的测试,
this.Close()
关闭了整个应用程序,我不得不使用
this.Hide()


此外,养成传递参数而不是设置全局变量的习惯也是很好的。几乎C#中的任何内容都可以以某种方式作为参数传递,这有助于生成更好、更清晰的代码

假设
vID
正确获取对象,您应该在Form2的构造函数中添加代表该数据类型的重载。然后像
Form2 NewForm=newform2(vID)那样传递它。传递参数后,最好
this.Close()
。@DavidBentley请用示例please发布一个答案Summing
vID
正确获取对象时,应在Form2的构造函数中添加该数据类型的重载代表。然后像
Form2 NewForm=newform2(vID)那样传递它。最好是
this.Close()
在传递参数后。@DavidBentley请介意用示例pleasethanks发布一个答案,我会尝试,但最后一件事是,在所有这些过程之后,
textbox
上分配给“textbox”的值有
leave
事件如何处理该事件?vID是否可以为null<代码>表格2新表格=新表格2(vID)因为我不总是要传递这个值,有时这个值可能是空的。谢谢,我会尝试一下,但最后一件事是,在处理完所有的
textbox
上分配给“textbox”的值后,有
离开
事件该事件如何处理?vID可以为空吗<代码>表格2新表格=新表格2(vID)表格2新表格=新表格2(vID)表格2新表格=新表格2(vID)