如何从 DataGridViewComboBox 的填充中选择一个项目
项目:Winforms,.NET框架:3.5(Visual Studio 2008)
我的问题是:
我有一个带有多个列的DataGridView
,其中一些类型和另一种类型DataGridViewTextBoxColumn
,< code>DataGridViewComboBoxColumn、DataGridViewComboBoxColumn
列由实体框架提供的实体填充。
// Example
((System.ComponentModel.ISupportInitialize)(this.EntityBindingSource)).BeginInit();
this.EntityBindingSource.DataSource = this.dtContext.ExampleEntity;
this.ComboColumn.DataSource = this.EntityBindingSource;
this.ComboColumn.DataPropertyName = "ExampleId";
this.ComboColumn.DisplayMember = "Example";
this.ComboColumn.ValueMember = "ExampleId";
当您运行并加载表单时,很好
但是当在运行时我想从 DataGridViewComboBoxColumn
集合中选择一个项目时,问题就出现了
,在事件 CellEndEdit
中
private void dgvDetalle_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = dgvDetalle.CurrentCell;
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)this.dgvDetalle.CurrentRow.Cells["ComboColumn"];
// Attempt 1
comboCell.Value = (ExampleEntity)comboTarifaImpuesto.Items[0];
// Attempt 2
comboCell.Value = ObjetoEntity;
// Attempt 3
comboCell.Value = "ValueExample"
// Attempt 4
comboCell.Value = ObjetoEntity.ToString();
}
- 尝试选择一个项目从组合框中
- 尝试选择实体的对象
- 尝试通过链或显示成员值进行选择
,但我无法选择所需的项目,出现异常:
DataGridViewComboBoxCell 值无效
Project: Winforms, .NET framework: 3.5 (Visual Studio 2008)
My problem is:
I have a DataGridView
with several columns, some of the type and another type DataGridViewTextBoxColumn
, DataGridViewComboBoxColumn
, DataGridViewComboBoxColumn
column is filled by an entity supplied by the Entity Framework.
// Example
((System.ComponentModel.ISupportInitialize)(this.EntityBindingSource)).BeginInit();
this.EntityBindingSource.DataSource = this.dtContext.ExampleEntity;
this.ComboColumn.DataSource = this.EntityBindingSource;
this.ComboColumn.DataPropertyName = "ExampleId";
this.ComboColumn.DisplayMember = "Example";
this.ComboColumn.ValueMember = "ExampleId";
When you run and load the form, fine
But the problem comes when at runtime I want to select an item from the collection of DataGridViewComboBoxColumn
That in the event CellEndEdit
private void dgvDetalle_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = dgvDetalle.CurrentCell;
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)this.dgvDetalle.CurrentRow.Cells["ComboColumn"];
// Attempt 1
comboCell.Value = (ExampleEntity)comboTarifaImpuesto.Items[0];
// Attempt 2
comboCell.Value = ObjetoEntity;
// Attempt 3
comboCell.Value = "ValueExample"
// Attempt 4
comboCell.Value = ObjetoEntity.ToString();
}
- Try selecting one item from combobox
- Try selecting an object of the entity
- Try selecting by means of a chain or Display Member Value
and I can not select the desired items, I get the exception:
DataGridViewComboBoxCell value is invalid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢另一个问题中提供的帮助:
DataGridViewComboBoxCell 绑定 - “值无效”
我可以修复它,管重新输入 DataGridViewComboBoxCell 的以下属性:
正如我在类型列 DataGridViewComboBoxColumn 中指定的那样
我已经正确使用了这个属性。价值
thanks to assistance provided in another question:
DataGridViewComboBoxCell Binding - "value is not valid"
I could fix it, tube to re-enter the following properties of DataGridViewComboBoxCell:
as I had specified in the type column DataGridViewComboBoxColumn
I already worked with this property correctly. Value