简体   繁体   中英

How to bind properly array to datagridview?

There are a Dependency class with some properties

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

     public Dependency() {}
}

and ProjectView class:

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

I want to bind array of Dependencies from ProjectView class to 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. 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? Thanks.

Okay, so let's say you had an array of those Dependency objects in memory somewhere, and you did something like this:

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.

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. 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.

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. So you have to use something like this:

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

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

From MSDN :

The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

  • The IList interface, including one-dimensional arrays.

  • The IListSource interface, such as the DataTable and DataSet classes.

  • The IBindingList interface, such as the BindingList<T> class.

  • The IBindingListView interface, such as the BindingSource class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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