如何从 DataGridViewComboBox 的填充中选择一个项目

发布于 2024-12-13 15:39:05 字数 1475 浏览 0 评论 0原文

项目: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-20 15:39:05

感谢另一个问题中提供的帮助:
DataGridViewComboBoxCell 绑定 - “值无效”

我可以修复它,管重新输入 DataGridViewComboBoxCell 的以下属性:

comboCell.DisplayMember="Example";
comboCell.ValueMember="ExampleId";

正如我在类型列 DataGridViewComboBoxColumn 中指定的那样
我已经正确使用了这个属性。价值

comboCell.Value = ObjetoEntity.ExampleId.ToString();

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:

comboCell.DisplayMember="Example";
comboCell.ValueMember="ExampleId";

as I had specified in the type column DataGridViewComboBoxColumn
I already worked with this property correctly. Value

comboCell.Value = ObjetoEntity.ExampleId.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文