[英]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: 您可以改进上述解决方案,但是概念是相同的:
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
. 将
DataGridViewComboBoxColumn
的DataSource
设置为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.