我用DataGridViewComboBoxColumn
在DataGridView
. 我需要的是ComboBox
根据每个 Row ID过滤值。
Si 它应该看起来像
-----------------------------
Row ID ComboBox
Values
ID Name
-----------------------------
1 1 A
2 B
-----------------------------
2 3 C
4 D
-----------------------------
所以我所做的是DataGridViewComboBoxColumn
用空创建,DataSource
如下面的代码所示
var d = new DataGridViewComboBoxColumn()
{
HeaderText = columnHeader,
Width = 50,
DataPropertyName = "D" + i.ToString(),
DataSource = new BindingSource(){ DataSource = new List<WorkTypeItem>() },
ValueMember = "ID",
DisplayMember = "Name",
Name = "D" + i.ToString()
};
dgvCalendar.Columns.Add(d);
我假设为事件中的每个单元格 填充Row 。 ComboBox
CellFormatting
private void DgvCalendar_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// Get object bounded with Row
CalendarDetailView row = dgvCalendar.Rows[e.RowIndex].DataBoundItem as CalendarDetailView;
// By row ID filter list for cell ComboBox
var workItems = MyWorkItems.Where(x => x.ID == row.ID);
// So how we can to populate cell ComboBox now?
dgvCalendar.Rows[e.RowIndex].Cells[1] ..... ??? = workItems ;
}
我这样做是因为我无法在单元格ComboBox中显示所有可能的值,因为它们不适合每一行。所以这个想法不是在ComboBox中显示 1000 个值,而是只显示Row ID引用的值。
我的目标是每个单元格都有自己的值列表,但来自同一个 List()。
任何线索如何做到这一点?
更新#1
感谢Marco Guignard ,我刚刚开始编写代码!我只是为 C# 版本更新它。
private void DgvCalendar_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
int currentKey = (int)dgvCalendar.CurrentRow.Cells[dgvCalendar.CurrentCell.ColumnIndex].Value;
ComboBox editor = (ComboBox)e.Control;
CalendarDetailView row = dgvCalendar.CurrentRow.DataBoundItem as CalendarDetailView;
var filteredItems = WorkTypes.Where(x =>x.ID = row.ID).ToList();
editor.DataSource = filteredItems;
editor.SelectedValue = currentKey;
}
}
更新#2
实际上,我已经用类似的方法完成了它,但使用了不同的事件。
因此,最初在列创建逻辑上,我使用数据库表中所有可能的记录来初始化DataGridView
值,并在DataGridView
出现时看到它。
当我们必须编辑组合框单元格时,我们只需应用对象允许值列表。
private void DgvCalendar_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (dgvCalendar.Columns[e.ColumnIndex].Name.StartsWith("D"))
{
var isComboBox = dgvCalendar.CurrentCell is DataGridViewComboBoxCell;
if (isComboBox)
{
DataGridViewComboBoxCell dgViewComboBoxCell = dgvCalendar.CurrentCell as DataGridViewComboBoxCell;
CalendarDetailView row = dgvCalendar.Rows[e.RowIndex].DataBoundItem as CalendarDetailView;
dgViewComboBoxCell.DataSource = new BindingSource()
{
DataSource = row.WorkTypes
};
}
}
}