简体   繁体   English

如何将DataGridViewComboBoxColumn绑定到父行的绑定对象的list属性?

[英]How do I bind a DataGridViewComboBoxColumn to the list property of the parent row's bound object?

Given the following classes: 给定以下类别:

public class Shirt
{
    public string Description { get; set; }
    public List<Color> ColorOptions { get; set; }
    public int SelectedColorId { get; set; }
}

public class Color
{
    public int Id { get; set; }
    public string Label { get; set; }
}

Why can't I get the combobox to show up in the DataGridView using the following code? 为什么不能使用以下代码将组合框显示在DataGridView中?

        List<Shirt> foundShirts = _dbShirtRepo.GetShirts();

        var nameColumn = new DataGridViewTextBoxColumn();
        nameColumn.DataPropertyName = "Description";
        nameColumn.HeaderText = "Description";

        var colorSelectColumn = new DataGridViewComboBoxColumn();
        colorSelectColumn.DataPropertyName = "ColorOptions";
        colorSelectColumn.DisplayMember = "Label";
        colorSelectColumn.ValueMember = "Id";


        dataGridView1.Columns.Add(nameColumn);
        dataGridView1.Columns.Add(colorSelectColumn);

        dataGridView1.DataSource = foundShirts;

Try something like this: 尝试这样的事情:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        DataGridViewComboBoxCell combo = this.dataGridView1[1, e.RowIndex] as DataGridViewComboBoxCell;
        combo.DataSource = ((Shirt)dataGridView1.Rows[e.RowIndex].DataBoundItem).ColorOptions;
    }
} 

As you can see, you have to provide DataSource per request because you can only have one at a time. 如您所见,您必须为每个请求提供DataSource 因为一次只能有一个。 There's no way to pre-set different data sources for each combo in each row. 无法为每一行中的每个组合预先设置不同的数据源。

You can improve the above solution but the concept is the same: 您可以改进上述解决方案,但是概念是相同的:

  • User clicks, hovers or otherwise "activates" the cell 用户单击,悬停或以其他方式“激活”单元格
  • Set up the DataSource of the column's combo before user has a chance to open it 在用户有机会打开列组合之前,设置其DataSource

This way user is not aware of any shenanigans going on and you can offer different choice for every individual row. 这样,用户不会知道发生了什么恶作剧,因此您可以为每一行提供不同的选择。

Note: I'm not 100% sure of how DataGridViewComboBoxCell operates, it's entirely possible that it caches and persists its data sources but experiment a bit before relying on it. 注意:我不是100%知道DataGridViewComboBoxCell运行方式,它完全有可能缓存并保留其数据源,但是在依赖它之前要进行一些试验。

You have not set the DataSource property for the DataGridViewComboBoxColumn . 您尚未为DataGridViewComboBoxColumn设置DataSource属性。 There are different ways to solve your problem. 有多种方法可以解决您的问题。

Set the DataSource of the DataGridViewComboBoxColumn to foundShirts.ColorOptions . DataGridViewComboBoxColumnDataSource设置为foundShirts.ColorOptions With this you get to see only colors available in the foundShirts list. 这样一来,您将只能在foundShirts列表中看到可用的颜色。

var colorSelectColumn = new DataGridViewComboBoxColumn();
colorSelectColumn.DataPropertyName = "ColorOptions";
colorSelectColumn.DisplayMember = "Label";
colorSelectColumn.ValueMember = "Id";
colorSelectColumn.DataSource = foundShirts.ColorOptions;

If you want to see all the possible colors you could have, prepare a separate list of all possible colors and set it as the DataGridViewComboBoxColumn DataSource . 如果要查看所有可能的颜色,请准备所有可能颜色的单独列表,并将其设置为DataGridViewComboBoxColumn DataSource

List<Color> allAvailableColorOptions = new List<Color>();
// add all the possible colors to this list.

var colorSelectColumn = new DataGridViewComboBoxColumn(); 
colorSelectColumn.DataPropertyName = "ColorOptions";   
colorSelectColumn.DisplayMember = "Label";
colorSelectColumn.ValueMember = "Id";
colorSelectColumn.DataSource = allAvailableColorOptions;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM