简体   繁体   English

如何正确绑定数组到datagridview?

[英]How to bind properly array to datagridview?

There are a Dependency class with some properties 有一个具有一些属性的Dependency类

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

     public Dependency() {}
}

and ProjectView class: 和ProjectView类:

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

I want to bind array of Dependencies from ProjectView class to DataGridView. 我想将从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;
     } 
  }

But when I'm binding like that, then exception occurs (AllowNew can only be set to true on an IBindingList or on a read-write list with a default public constructor.), because _currentProjectView.Dependencies is array, and it can't be able to add new items. 但是当我这样绑定时,就会发生异常(AllowNew只能在IBindingList或带有默认公共构造函数的读写列表上设置为true。),因为_currentProjectView.Dependencies是数组,它不能能够添加新项目。 There is solution is convert to list, but it is not convenient, because it's just copy and lost reference to origin array. 有解决方案是转换为列表,但它不方便,因为它只是复制和丢失对原始数组的引用。 Is there solution of this problem? 有这个问题的解决方案吗? How to bind properly array to datagridview? 如何正确绑定数组到datagridview? Thanks. 谢谢。

Okay, so let's say you had an array of those Dependency objects in memory somewhere, and you did something like this: 好吧,那么让我们说你在某个地方有一些Dependency对象的数组,你做了类似这样的事情:

arrayOfObjs.ToList();

That's not going to change the reference they point to. 这不会改变他们指向的参考。 So, to further that point, the array they came from, if it were persisted in memory, it would see the changes made. 因此,为了进一步说明它们来自的数组,如果它存在于内存中,它会看到所做的更改。 Now, will you see any additions, no? 现在,你会看到任何补充,不是吗? But that's why you use a mutable type like a List instead of an array. 但这就是为什么你使用像List这样的可变类型而不是数组。

So, my recommendation is that you do this: 所以,我的建议是你这样做:

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

And dump the array. 并转储数组。 If the original list of Dependency is coming from an array, well then just issue the ToList method on it and dump it. 如果Dependency的原始列表来自数组,那么只需在其上发出ToList方法并将其转储。 When you want to get an array back out of it (maybe you need to pass an array back), just do this: 当你想要从中取出一个数组时(也许你需要传回一个数组),就这样做:

projectViewObj.Dependencies.ToArray();

That will build Dependency[] for you. 那将为你构建Dependency[]

EDIT 编辑

So consider the following structure: 请考虑以下结构:

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

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

Then in the form: 然后在表格中:

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

And then when editing is finished: 编辑完成后:

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

I think you can't bind an array to DataGridView without using collections. 我认为你不能在不使用集合的情况下将数组绑定到DataGridView So you have to use something like this: 所以你必须使用这样的东西:

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

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

From MSDN : 来自MSDN

The DataGridView class supports the standard Windows Forms data-binding model. DataGridView类支持标准的Windows窗体数据绑定模型。 This means the data source can be of any type that implements one of the following interfaces: 这意味着数据源可以是实现以下接口之一的任何类型:

  • The IList interface, including one-dimensional arrays. IList接口,包括一维数组。

  • The IListSource interface, such as the DataTable and DataSet classes. IListSource接口,例如DataTableDataSet类。

  • The IBindingList interface, such as the BindingList<T> class. IBindingList接口,例如BindingList<T>类。

  • The IBindingListView interface, such as the BindingSource class. IBindingListView接口,例如BindingSource类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM