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

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

问题描述

我的 DataGridView 的DataSource是一个 BindingList< Filter> ,其中包含一个枚举。 gridview只包含两列:字符串的常规文本框列和枚举的组合框(下拉列)。如果我将combobox列绑定到我的对象的枚举变量,我会收到一个错误。这是我的对象的代码:

  public class FilterProfile 
{
public FilterProfile()
{
filters = new BindingList< Filter>(); //绑定到gridview的列表
}
public string name {get;组; }
public BindingList< Filter>过滤器{get;组;
}

public class Filter
{
public string keyword {get;组; }
public FilterType type {get;组; } //有问题的枚举
}

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

我有一个表单,用户选择一个 FilterProfile ,然后从全局列表中找到相应的FilterProfile,并绑定它:

  foreach(_filterProfiles中的PlvFilterProfile filterProfile)
{
//找到正确的过滤器配置文件
if(filterProfile.name.Equals(lstFilterProfiles.Text))
{
// bind it
grdFilters.DataSource = filterProfile.filters;
break;
}
}

为了使DataGridView中所做的更改为反映在 filterProfile.filters 中我需要将两列的 DataPropertyName 属性设置为它们各自的变量(关键字类型)。这对关键字字符串正常工作,但不适用于类型枚举。



如果我保留行 colFilterType.DataPropertyName =type; 每当新行时,我会收到错误是创建或每当我把鼠标放在下拉列表。如果我摆脱它,每个新创建的过滤器的类型设置为 0 ,从未更新。 p>



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

解决方案

问题是当您专注于新行(准备添加新行)时,底层列表中需要一个新对象,该对象默认为 null ,该值绑定到新行,当然, ComboBoxCell 不能接受该空值,导致您遇到的异常。解决方案确实很简单,我们只需要处理 BindingList 的事件 AddingNew ,设置默认的新对象在那里有一个有效的值,然后它工作正常:

  public FilterProfile()
{
filters = new BindingList< Filter>(); //绑定到gridview
的列表filters.AddingNew + =(s,e)=> {
// FilterType的默认值取决于您。
e.NewObject = new Filter {type = FilterType.SESSION};
};
}


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
}

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;
        }
     }

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.

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.

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

解决方案

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天全站免登陆