C# 双击datagridview中的行,获取特定单元格的值并将其传递给另一个窗体
我只是想知道下面的情况是否正确 我正在努力做到以下几点 1-从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
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发布一个答案SummingvID
正确获取对象时,应在Form2的构造函数中添加该数据类型的重载代表。然后像Form2 NewForm=newform2(vID)那样传递它代码>。最好是this.Close()
在传递参数后。@DavidBentley请介意用示例pleasethanks发布一个答案,我会尝试,但最后一件事是,在所有这些过程之后,textbox
上分配给“textbox”的值有leave
事件如何处理该事件?vID是否可以为null<代码>表格2新表格=新表格2(vID)代码>因为我不总是要传递这个值,有时这个值可能是空的。谢谢,我会尝试一下,但最后一件事是,在处理完所有的textbox
上分配给“textbox”的值后,有离开
事件该事件如何处理?vID可以为空吗<代码>表格2新表格=新表格2(vID)因为我不总是要传递这个值,有时这个值可能是空的,所以vID可以是空的吗<代码>表格2新表格=新表格2(vID)因为我不总是要传递这个值,有时这个值可能是空的,所以vID可以是空的吗<代码>表格2新表格=新表格2(vID)因为我不总是要传递这个值,所以有时候这个值可能是空的