【问题标题】:Passing data from a column in a DataGridView to a combobox in another form将数据从 DataGridView 中的列传递到另一种形式的组合框
【发布时间】:2019-05-15 12:15:08
【问题描述】:

我希望 DataGridView 中第 2 列(所有行)的数据成为另一种形式的组合框的输入。我尝试过的以下代码包含 2 个错误comboBox1 在当前上下文中不存在 并且非静态字段需要对象引用。下面是我的代码。

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

【问题讨论】:

    标签: c# combobox datagridviewcolumn


    【解决方案1】:

    当您想要传递信息集合时,您需要使用适当的类型。例如 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 值通过公共属性传递给第二个实例,并在该属性的 set 访问器中更改内部组合框的内容1

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

    【讨论】:

      【解决方案2】:

      首先定义Form2的公共参数

      public String data;
      

      然后在Form1中打开Form2时设置数据值如下:

      Form2 form2 = new Form2();
      form2.data = your_form1_data;
      form2.Show();
      

      现在您在 Form2 中有了数据值。

      【讨论】:

        猜你喜欢
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-23
        相关资源
        最近更新 更多