将数据从 DataGridView 中的列传递到另一种形式的组合框
Passing data from a column in a DataGridView to a combobox in another form
我希望 DataGridView 中第 2 列(所有行)的数据作为另一种形式的组合框的输入。我试过的下面的代码包含 2 个错误 comboBox1 在当前上下文中不存在 并且 非静态字段需要对象引用 。下面是我的代码。
表单 1(带有 DataGridView 和按钮)
// put as public string as the DataGridView rows will keep updating
public string data;
public Form1()
{
InitializeComponent();
}
//button to go Form 2 which contains the combobox
private void Button1_Click(object sender, EventArgs e)
{
string data = string.Empty;
int indexOfYourColumn = 2;
foreach (DataGridViewRow row in dataGridView1.Rows)
data = row.Cells[indexOfYourColumn].Value.ToString();
comboBox1.Items.Add(data);
this.Hide();
FormsCollection.Form2.Show();
}
Form2(带组合框)
//put as public to obtain value from Form 1
public string data;
public Form 2()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Text = Form1.data;
//not to repeat the value entered if a particular value has been entered
String s = data;
if (!comboBox1.Items.Contains(s))
{
comboBox1.Items.Add(s);
}
}
首先为表格 2 定义 public 参数
public String data;
然后在打开 Form 2 时在 Form 1 中设置数据值,如下所示:
Form2 form2 = new Form2();
form2.data = your_form1_data;
form2.Show();
现在您在 Form2 中有了数据值。
当您想要传递一组信息时,您需要使用适当的类型。例如 List<string>
不是简单的字符串。然后你创建或获取第二种形式的实例,只有在你拥有第二种形式的实例之后,你才能给它提供要显示的数据集合
private void Button1_Click(object sender, EventArgs e)
{
// These is where you store the elements to pass to the Form2 instance
List<string> data = new List<string>();;
int indexOfYourColumn = 2;
// Build the collection from the selected column for each row
foreach (DataGridViewRow row in dataGridView1.Rows)
data.Add(row.Cells[indexOfYourColumn].Value.ToString());
this.Hide();
// pass your data to the public property of the Form2 instance
Form2 f = FormsCollection.Form2;
f.Data = data;
f.Show();
}
如您所见,data 值通过 public 属性 传递给第二个实例,并且在 [=20] 的集合访问器中=] 你改变了内部combobox1的内容
private List<string> _data;
public List<string> Data
{
get { return _data; }
set
{
_data = value;
// This code uses the DataSource property of the combobox
// combobox1.DataSource = null;
// combobox1.DataSource = value;
// This code works directly with the Items collection of the combo
combobox1.Items.Clear();
foreach(string s in _data)
combobox1.Items.Add(s);
}
};
public Form 2()
{
InitializeComponent();
}
...
我希望 DataGridView 中第 2 列(所有行)的数据作为另一种形式的组合框的输入。我试过的下面的代码包含 2 个错误 comboBox1 在当前上下文中不存在 并且 非静态字段需要对象引用 。下面是我的代码。
表单 1(带有 DataGridView 和按钮)
// put as public string as the DataGridView rows will keep updating
public string data;
public Form1()
{
InitializeComponent();
}
//button to go Form 2 which contains the combobox
private void Button1_Click(object sender, EventArgs e)
{
string data = string.Empty;
int indexOfYourColumn = 2;
foreach (DataGridViewRow row in dataGridView1.Rows)
data = row.Cells[indexOfYourColumn].Value.ToString();
comboBox1.Items.Add(data);
this.Hide();
FormsCollection.Form2.Show();
}
Form2(带组合框)
//put as public to obtain value from Form 1
public string data;
public Form 2()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Text = Form1.data;
//not to repeat the value entered if a particular value has been entered
String s = data;
if (!comboBox1.Items.Contains(s))
{
comboBox1.Items.Add(s);
}
}
首先为表格 2 定义 public 参数
public String data;
然后在打开 Form 2 时在 Form 1 中设置数据值,如下所示:
Form2 form2 = new Form2();
form2.data = your_form1_data;
form2.Show();
现在您在 Form2 中有了数据值。
当您想要传递一组信息时,您需要使用适当的类型。例如 List<string>
不是简单的字符串。然后你创建或获取第二种形式的实例,只有在你拥有第二种形式的实例之后,你才能给它提供要显示的数据集合
private void Button1_Click(object sender, EventArgs e)
{
// These is where you store the elements to pass to the Form2 instance
List<string> data = new List<string>();;
int indexOfYourColumn = 2;
// Build the collection from the selected column for each row
foreach (DataGridViewRow row in dataGridView1.Rows)
data.Add(row.Cells[indexOfYourColumn].Value.ToString());
this.Hide();
// pass your data to the public property of the Form2 instance
Form2 f = FormsCollection.Form2;
f.Data = data;
f.Show();
}
如您所见,data 值通过 public 属性 传递给第二个实例,并且在 [=20] 的集合访问器中=] 你改变了内部combobox1的内容
private List<string> _data;
public List<string> Data
{
get { return _data; }
set
{
_data = value;
// This code uses the DataSource property of the combobox
// combobox1.DataSource = null;
// combobox1.DataSource = value;
// This code works directly with the Items collection of the combo
combobox1.Items.Clear();
foreach(string s in _data)
combobox1.Items.Add(s);
}
};
public Form 2()
{
InitializeComponent();
}
...