【问题标题】:How to bind properly array to datagridview?如何将数组正确绑定到datagridview?
【发布时间】:2012-11-02 11:53:25
【问题描述】:

有一个具有一些属性的依赖类

class Dependency:
{
     public string ArtifactId { get; set; }
     public string GroupId { get; set; }
     public string Version { get; set; }

     public Dependency() {}
}

和 ProjectView 类:

 class ProjectView:
 {
     public string Dependency[] { get; set; }
        ...
 }

我想将 ProjectView 类中的 Dependencies 数组绑定到 DataGridView。

 class Editor
 {
     private readonly ProjectView _currentProjectView;

     ... //I skipped constructor and other methods  

     private void PopulateTabs()
     {
        BindingSource source = new BindingSource {DataSource = _currentProjectView.Dependencies, AllowNew = true};
        dataGridViewDependencies.DataSource = source;
     } 
  }

但是当我这样绑定时,就会发生异常(AllowNew 只能在 IBindingList 或具有默认公共构造函数的读写列表上设置为 true。),因为 _currentProjectView.Dependencies 是数组,它无法添加新项目。 有解决方案是转换为列表,但不方便,因为它只是复制并丢失了对原始数组的引用。这个问题有解决方案吗?如何将数组正确绑定到datagridview? 谢谢。

【问题讨论】:

  • 对我来说,在填充 Datagrid 时,我总是使用 DataTable 或 List 对象,我永远不会使用 Array。为什么要完全使用数组?
  • 但我不想更改 ProjectView 类

标签: c# .net winforms datagridview


【解决方案1】:

好的,假设您在内存中的某处有一个由 Dependency 对象组成的数组,并且您执行了以下操作:

arrayOfObjs.ToList();

这不会改变他们指向的引用。因此,更进一步,他们来自的数组,如果它持久存在于内存中,它将看到所做的更改。现在,你会看到任何补充,不是吗?但这就是为什么你使用像 List 这样的可变类型而不是数组的原因。

所以,我的建议是你这样做:

class ProjectView:
{
    public string List<Dependency> Dependencies { get; set; }
}

然后转储数组。如果Dependency 的原始列表来自一个数组,那么只需对其发出ToList 方法并转储它。当你想从中取出一个数组(也许你需要传回一个数组),只需这样做:

projectViewObj.Dependencies.ToArray();

这将为您构建Dependency[]

编辑

所以考虑以下结构:

class ProjectView:
{
    public Dependency[] Dependencies { get; set; }

    public List<Dependency> DependencyList { get { return this.Dependencies.ToList(); } }
}

然后是形式:

private void PopulateTabs()
{
   BindingSource source = new BindingSource { DataSource = _currentProjectView.DependencyList, AllowNew = true};
   dataGridViewDependencies.DataSource = source;
}

然后当编辑完成时:

_currentProjectView.Dependencies = _currentProjectView.DependencyList.ToArray();

【讨论】:

  • 我不想更改 ProjectView 类,因为我使用 PropertyGrid 并且 Dependency 属性视图不适合。
  • @AndrewOrlov,您可以尝试的一件事是在ProjectView 类上将其公开为IList&lt;Dependency&gt;。我相信PropertyGrid 会回复同一个设计师,因为它的实际值是一维数组。
  • 它没有用。属性网格已停止显示依赖属性。
  • @AndrewOrlov,好的,所以我认为您有两个选择。第一个是向ProjectView 添加一个新属性,它只是一个get,实际上只返回Dependencies.ToList()——我喜欢那个,因为它也维护了你需要的数组。或者您可以在设置DataGridViewDataSource 时构建列表。
  • 在构建 DataSource 期间如何帮助我构建列表?我所能做的就是编辑依赖关系,但如果我要添加新的,那么它就不会保存。
【解决方案2】:

我认为你不能在不使用集合的情况下将数组绑定到DataGridView。所以你必须使用这样的东西:

BindingSource source = new BindingSource {DataSource = _currentProjectView.Dependencies, AllowNew = true};

dataGridViewDependencies.DataSource = _currentProjectView.Dependencies.ToList();

来自MSDN

DataGridView 类支持标准的 Windows 窗体数据绑定模型。这意味着数据源可以是实现以下接口之一的任何类型:

  • IList 接口,包括一维数组。

  • IListSource 接口,例如DataTableDataSet 类。

  • IBindingList 接口,如BindingList&lt;T&gt; 类。

  • IBindingListView 接口,如BindingSource 类。

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 2014-09-15
    • 2016-11-09
    • 2016-08-03
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2018-03-29
    相关资源
    最近更新 更多