将DataGridViewComboBoxColumn绑定到嵌套列表 [英] Bind DataGridViewComboBoxColumn to a nested List

查看:40
本文介绍了将DataGridViewComboBoxColumn绑定到嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下属性的类:

I have a class with the following properties:

Public string Name { get; set; }
Public string LastName { get; set; }
Public List<string> Jobs{ get; set; }

属性 Name LastName 绑定到 DataGridView ,而我需要将 Jobs 绑定到 DataGridView 内的 ComboBox .

The properties Name and LastName are bound to a DataGridView, while I need to bind Jobs to a ComboBox inside the DataGridView.

  • 如果该人在 ComboBox 中有1个工作,则应该只显示数字1.
  • 如果该人有2个工作,则在单击 ComboBox 时应显示1和2.
  • If the person has 1 job in the ComboBox it should appear with only the number 1.
  • If the person has 2 jobs it should appear with 1 and 2 when clicking on the ComboBox.

例如:

John Smith 1
Paul Mayer 1
           2

这是我添加 ComboBoxColumn 的方式:

private void AddComboBox()
{
    DataGridViewComboBoxColumn Jobs = new DataGridViewComboBoxColumn();
    foreach (var item in People)
    {
        for (int i = 0; i < item.Jobs.Count; i++)
        {
            Jobs.Items.Add(item.Jobs[i]);
        }
    }
}

但是此代码使每个 ComboBox 都相同,因此我得到了1-1-2,而不是在第一个 ComboBox 中有1-2,在第二个 ComboBox 中有1-2.每个 ComboBox .

But this code makes every ComboBox the same, so instead of having 1 in the first and 1-2 in the second ComboBox, I get 1-1-2 for each ComboBox.

有没有一种方法来获取当前的 ComboBox 来添加项目?不幸的是,由于它是一个 ComboBoxColumn ,所以我没有像单元格那样的 Rows [i] 属性.

Is there a way to get the current ComboBox for adding items? Unfortunately, since it is a ComboBoxColumn I don't have the Rows[i] property like the cells.

我也尝试了数据绑定,但是 Jobs 是一个列表,因此 DataPropertyName 会由于从列表到字符串的转换而引发错误.

I also tried with a databinding but Jobs is a list so DataPropertyName throws an error due to the conversion from list to string.

感谢所有人.

推荐答案

您可以通过在 DataGridView 完成其自身的源绑定之后采购每个 ComboBox 来使用数据绑定.

You can use data binding by sourcing each ComboBox after the DataGridView has completed its own source binding.

  1. 完全删除 AddComboBox ,您将不需要它.
  2. 添加以下事件处理程序 dataGridViewJobs.DataBindingComplete + = DataGridViewJobs_DataBindingComplete; ,并在其中获取每个 DataGridViewComboBoxCell :

  1. Remove AddComboBox completely, you won't be needing it.
  2. Add the following event handler, dataGridViewJobs.DataBindingComplete += DataGridViewJobs_DataBindingComplete; and within it source each DataGridViewComboBoxCell:

private void DataGridViewJobs_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    string colName = "Jobs";

    if (dataGridViewJobs.Columns.Contains(colName))
    {
        dataGridViewJobs.Columns.Remove(colName);
    }

    DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
    column.Name = colName;
    dataGridViewJobs.Columns.Add(column);

    foreach (DataGridViewRow row in dataGridViewJobs.Rows)
    {
        var person = (Person)row.DataBoundItem;
        var cell = row.Cells[colName] as DataGridViewComboBoxCell;

        cell.DataSource = person.Jobs;
        cell.Value = person.Jobs[0];
    }
}

这篇关于将DataGridViewComboBoxColumn绑定到嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆