【问题标题】:datagrid with Comboboxcolumn - AddNew Error带有 Comboboxcolumn 的数据网格 - AddNew 错误
【发布时间】:2012-05-24 08:06:38
【问题描述】:

使用 EF 4、C# WinForms 和 Northwind 数据库,我将数据网格绑定到 Orders 实体,然后在运行时创建了一个新的 Comboboxcolum。

    Entities NW;
    private void Form1_Load(object sender, EventArgs e)
    {
        NW = new Entities();
        BindingSource1.DataSource = NW.Orders;

        DataGridViewComboBoxColumn CboCol = new DataGridViewComboBoxColumn()
        {
            HeaderText = "Customer",
            DataPropertyName = " Customer ID",
            DataSource = NW.Customers.Execute(MergeOption.AppendOnly),
            ValueMember = "CustomerID",
            DisplayMember = "CompanyName"
        };
        Datagriview1.Columns.Insert(1, CboCol);
    }

使用上面的代码,我成功地改变了datagridview中的值并删除了行,但是无法通过单击“+”按钮(添加新)来添加新行,错误:“datagridviewcombocell不是有效值”。请问有什么办法吗?

【问题讨论】:

  • 在那个“+”按钮的点击事件上运行什么代码?错误发生在哪一行?
  • databindingNavigator 添加按钮,无附加代码,编译器未指示行号

标签: c# winforms entity-framework datagridview datagridviewcombobox


【解决方案1】:

因为你将DataGridViewComboBoxColumn绑定了数据,所以当你添加一个包含一些空值的新行时,在你的数据源中没有找到,这就是为什么它提示你不是一个有效值

我的建议不是直接将新记录添加到 DataGirdViewComboBox 中,而是您绑定到组合框中的数据源。从数据源绑定 DataGridViewComboBox 时,最好始终将其保持为只读状态。

【讨论】:

  • datagridview在设计时绑定到数据绑定源,databindingNavigator1.AddNewItem = bindingNavigatorAddNewItem的属性,我尝试将DataGridViewComboBox设置为只读,结果还是一样。
  • 我试着重做这个更简单,请看这个:[stackoverflow.com/questions/10657095/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 2023-03-18
相关资源
最近更新 更多