0

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.

2
  • 1
    For me, I'd always use either a DataTable or a List object when populating Datagrid's, I wouldn't ever use an Array. why is it you want to use an Array exactly? Commented Nov 2, 2012 at 12:12
  • But I would not want to change ProjectView class Commented Nov 2, 2012 at 12:15

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

I would not want to change ProjectView class, because I use PropertyGrid and Dependency property view doesn't fit.
@AndrewOrlov, one thing you could try is exposing it as an IList<Dependency> on the ProjectView class. I believe the PropertyGrid will respond with the same designer because it's actual value is a one-dimensional array.
It didn't work. Property grid has stopped to show Dependency property.
@AndrewOrlov, alright so I think you have two options. The first is to add a new property to ProjectView that is a get only and literally just returns Dependencies.ToList() - I like that one because it maintains the array you need too. Or you can build the list when you set the DataSource of the DataGridView.
how can help me a building the list during of build DataSource? All that I can, it is edit Dependencies, but if I'll add new one, then it will not saved.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.