Re: Deleting ArrayList element from DataGrid
- From: "Pete Davis" <pdavis68@[nospam]hotmail.com>
- Date: Thu, 17 Nov 2005 10:17:38 -0600
The problem is that the grid doesn't get updates from an ArrayList after
it's first bound. So when you bind the grid to an arraylist with say, 100
items, it always thinks that array list has 100 items.
You should create your own collection class derived from IBindingList and
then implement the ListChanged event. You can store everything underneath in
an array list and simply call through to the array list for all the method
implementations.
It's pretty simple to do and this will give you the kind of functionality
you're looking for.
Optionally, you can try doing the following, after modifying the ArrayList's
contents:
(BindingContext[grid.DataSource] as CurrencyManager).Refresh();
But this is really sort of a hack. Implementing IBindingList is definitely
the preferable solution.
Pete
<nadeem_far@xxxxxxxxx> wrote in message
news:1132238702.964739.105720@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hello All,
>
> I have been trying to solve the following issue and it seems like there
> is no solution available any where and maybe people at Microsoft should
> may be of some help.
>
> I am writing a .Net desktop application using C# and framework version
> 1.1
>
> I have a arraylist of some objects of the same type and I am showing
> them in a datagrid using
>
> Daragrid1. DataSource = objArray;
>
> When I try to delete any of the rows from the datagrid I get the
> following error message.
>
> An unhandled exception of type 'System.IndexOutOfRangeException'
> occurred in system.windows.forms.dll
>
> Additional information: No value at index 1
>
> Here is a code snippet
>
> public class Customer
> {
>
> private string _customerName;
>
> public Customer (string name)
> {
> _customerName = name;
> }
> public string CustomerName
> {
> get { return _customerName ;}
> set { _customerName = value;}
> }
> }
>
> public class Customerlist
> {
>
> public ArrayList customerList;
>
> Customerlist()
> {
> customerList = new ArrayList();
> customerList.Add(new Customer ("John"));
> customerList.Add(new Customer ("Doe"));
> customerList.Add(new Customer ("Dave"));
> }
>
> public void Remove (int i )
> {
> customerList.RemoveAt(i);
> }
> }
>
> In my form I create a variable of Customerlist and bind it to the grid
> as
> dg1.DataSource = Customerlist.customerList;
>
> I have a button, when I select a row in the grid and hit Delete I do
> the following
>
> dg1.DataSource = null;
> customerList.Remove ( dg1.currentRowIndex);
> dg1.DataSource = Customerlist.customerList;
>
> After this code is called I get the above error. Has anyone solved this
> problem. I will be thankful if some one can give me the solution.
>
> Thanks
>
> nad
>
.
- Follow-Ups:
- Re: Deleting ArrayList element from DataGrid
- From: nadeem_far
- Re: Deleting ArrayList element from DataGrid
- References:
- Deleting ArrayList element from DataGrid
- From: nadeem_far
- Deleting ArrayList element from DataGrid
- Prev by Date: Re: StreamReader to read from string?
- Next by Date: Re: log4Net.dll
- Previous by thread: Deleting ArrayList element from DataGrid
- Next by thread: Re: Deleting ArrayList element from DataGrid
- Index(es):