473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to bind arraylist to datagridview

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
3 15863
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Peter Afonin | last post by:
Hello, I have a code that queries Active Directory and put logins and names into the ArrayList. Then I need to bind this ArrayList to the DropdDownList, making names as DataTextField and logins...
2
by: Morten Hauge | last post by:
Hi! I have a problem, I'm not sure if this is the proper way to do it, but I have the following scenario: I want to list all categories, and all products in that category whenever I select a...
3
by: serge calderara | last post by:
Dear all, Does anyone know how to bind a System.Collection.ArraysList object to a Dataset ? Thanks for your reply Regards Serge
3
by: sck10 | last post by:
Hello, I am trying to bind an arraylist to a FormView DropDownList control in the PreRender state. The error that I get is the following: Databinding methods such as Eval(), XPath(), and...
7
by: Mike | last post by:
i have a small difficulties with BindingSource and dataGridView bind db has properly opened and bind doesn't works. Unfortunately I didn't find any good example how to connect MS Access with...
4
by: Pieter | last post by:
Hi, I want to bind a proeprty of a child object to a DataGridView, but it doesn't work. For instance: Imagen that I have an object Company, and this object company has a child object...
0
by: pbd22 | last post by:
Hi. I am returning to an old bit of code in our program and need to figure out how to sort my columns on bind. I am sorting on Date (mostly) and some other values. Problem is, the code is an...
2
by: Vic | last post by:
I have a ArrayList bound to a DataGridView. The objects in de arryalist have 5 propertie but i need only 3 of then in de datagridview. Cane somebody tell me how to prevent that some of the...
0
by: mwcapps | last post by:
I have built an arraylist and have used it to fill an editable datagridview. Now I need to know if I can update the arraylist with whatever edits were made to the datagridview or if the edits need...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.