DataGridView 不会更新 BindingList 中的 Enum [英] DataGridView doesn't update Enum in BindingList

查看:18
本文介绍了DataGridView 不会更新 BindingList 中的 Enum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 DataGridView 的数据源是一个 BindingList,它包含一个枚举.gridview 仅包含两列:用于字符串的常规文本框列和用于枚举的组合框(下拉)列.如果我将组合框列绑定到对象的枚举变量,则会出现错误.这是我的对象的代码:

The DataSource of my DataGridView is a BindingList<Filter> which contains an enum. The gridview contains only two columns: a regular textbox column for a string, and a combobox (drop down) column for the enum. If I bind the combobox column to the enum variable of my object I get an error. Here is the code for my objects:

public class FilterProfile
{
   public FilterProfile()
   {
      filters = new BindingList<Filter>();  // the list that gets bound to gridview
   }
   public string name { get; set; }
   public BindingList<Filter> filters { get; set; }
}

public class Filter
{
  public string keyword { get; set; }
  public FilterType type { get; set; }  // the enum in question
}

public enum FilterType : int
{
  SESSION = 1,
  ORDER = 2,
  SHIPMENT = 3
}

我有一个表单,用户可以在其中从下拉菜单中选择 FilterProfile,然后从全局列表中找到合适的 FilterProfile 并绑定它:

I have a form where the user selects a FilterProfile from a dropdown menu and then I find the appropriate FilterProfile from a global list and bind it:

foreach (PlvFilterProfile filterProfile in _filterProfiles)
     {  
        // find the correct filter profile
        if (filterProfile.name.Equals(lstFilterProfiles.Text))
        {
           // bind it
           grdFilters.DataSource = filterProfile.filters;
           break;
        }
     }

为了使 DataGridView 中所做的更改反映在 filterProfile.filters 中,我需要将两列的 DataPropertyName 属性设置为它们各自的变量(<代码>关键字或<代码>类型).这适用于 keyword 字符串,但不适用于 type 枚举.

In order for the changes made in the DataGridView to be reflected in filterProfile.filters I need to set the DataPropertyName attribute of both columns to their respective variable (either keyword or type). This works correctly for the keyword string, but not with the type enum.

如果我保留行 colFilterType.DataPropertyName = "type"; 每当创建新行或将鼠标放在下拉列表上时,我都会收到以下错误.如果我去掉它,每个新创建的过滤器的 type 都会设置为 0 并且永远不会更新.

If I keep the line colFilterType.DataPropertyName = "type"; I get the error below whenever a new row is created or whenever I put my mouse over the dropdown. If I get rid of it, the type of every newly created Filter is set to 0 and never updated.

我不确定是什么导致了 DataError 事件,所以不知道如何处理它或在哪里断点.

I'm not sure what causes the DataError event so don't know how to handle it or where to breakpoint.

推荐答案

问题是当你关注新行时(准备添加新行),底层列表中需要一个新对象,这个对象是默认的通过 null,该值绑定到新行,当然 ComboBoxCell 不能接受该空值,从而导致您遇到的异常.解决方案确实很简单,我们只需要处理BindingListAddingNew事件,将里面的默认新对象设置为一个有效值,然后就可以正常工作了:

The problem is when you focus on the new row (prepare to add a new row), a new object is required in the underlying list, this object is default by null, that value is bound to the new row and of course the ComboBoxCell can't accept that null value, causing the exception as you encountered. The solution is very simple indeed, we just need to handle the event AddingNew of the BindingList, set the default new object in there to a valid value and then it works just fine:

public FilterProfile()
{
  filters = new BindingList<Filter>();  // the list that gets bound to gridview
  filters.AddingNew += (s,e) => {
    //the default value of FilterType is up to you.
    e.NewObject = new Filter {type = FilterType.SESSION };
  };
}

这篇关于DataGridView 不会更新 BindingList 中的 Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆