By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,207 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,207 IT Pros & Developers. It's quick & easy.

how to bind arraylist to datagridview

P: n/a
Purpose: The objective is to update or add a new row in datagridview
using an arraylist

I have an arraylist inside of a class and i added an object to the
arraylist on the button click event. Then, after clearing the
datagridview1, i re-assigned the arraylist to the datasource property.
But it does not work, since the application crashes. The code goes
something like this:

Node n1=new Node();
State st1=new State("State",1);
n1.statesList.Add(s1);
datagridview1.DataBindings.Clear();
datagridview1.Refresh();
datagridview1.Datasource=n1.stateList; // Damn!!!! it crashes ..

Need help
Amir Diwan

Oct 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Amir,

Well, what is the exception you are getting?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"weird0" <am********@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
Purpose: The objective is to update or add a new row in datagridview
using an arraylist

I have an arraylist inside of a class and i added an object to the
arraylist on the button click event. Then, after clearing the
datagridview1, i re-assigned the arraylist to the datasource property.
But it does not work, since the application crashes. The code goes
something like this:

Node n1=new Node();
State st1=new State("State",1);
n1.statesList.Add(s1);
datagridview1.DataBindings.Clear();
datagridview1.Refresh();
datagridview1.Datasource=n1.stateList; // Damn!!!! it crashes ..

Need help
Amir Diwan
Oct 16 '07 #2

P: n/a
Hi,

You can bind almost any collection to an grid.
What error are you getting?

The most common error is an incorrect name of a column/property

--
Ignacio Machin
www.laceupsolutions.com
Mobile & warehouse Solutions.
"weird0" <am********@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
Purpose: The objective is to update or add a new row in datagridview
using an arraylist

I have an arraylist inside of a class and i added an object to the
arraylist on the button click event. Then, after clearing the
datagridview1, i re-assigned the arraylist to the datasource property.
But it does not work, since the application crashes. The code goes
something like this:

Node n1=new Node();
State st1=new State("State",1);
n1.statesList.Add(s1);
datagridview1.DataBindings.Clear();
datagridview1.Refresh();
datagridview1.Datasource=n1.stateList; // Damn!!!! it crashes ..

Need help
Amir Diwan

Oct 16 '07 #3

P: n/a
First, check your columns ;-p

However, since you are using DataGridView (and hence 2.0) the easiest
answer might actually be to move to List<T(for suitable T) - for
reasons that I will explain:

First; two questions:
Have you got mixed data? (i.e. different types of objects in the
ArrayList)
Is your ArrayList empty when you bind it?

Binding to lists expects the list contents to be similar. This is
particularly important with grids for obvious reasons.
The binding code works by checking a series of interfaces, including
IListSource, ITypedList, etc. Near the end of possibilities, are:
* IList and a typed indexer (not object)
* IList and at least on row

ArrayList (being untyped) fails the first of my bullets, so it has to
check for contents; it then has to assume that the first (zeroth,
depending on your perspective) item in the list is similar, so it uses
the Type of this item to infer the properties (via
TypeDescriptor.GetProperties(item[0].GetType() IIRC). Of course, it
can only do this if there is something in the list.
List<T>, by contrast, has a typed indexer "public T this[int index]
{...}"; this means that can stop at the first bullet, using (loosely)
TypeDescriptor.GetProperties(typeof(T)).

As such, the typed lists are significantly more reliable for binding.
Plus it may save you significant grief during development as you get
much better IDE support and need to do less casting.

Without more info I can't say for sure if this will help, but it is
worth a shot.

Finally, if you want real-time updates from objects (multiple views),
try using BindingList<Tassuming that your T implements
INotifyPropertyChange.

Marc

Oct 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.