473,397 Members | 2,056 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,397 software developers and data experts.

Deleting ArrayList element from DataGrid

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

Nov 17 '05 #1
5 4470
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

<na********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 17 '05 #2
Hi Pete,

Thanks for the help but I will need some more help in using
IBindingList.
I will prefer this way but I could not get much help from MSDN
regarding the implementation of IBindlingList.

some more help regarding such implementation will be nice.

Thanks
Nad

Nov 18 '05 #3
Simply create your class::

public class MyCollection : IBindingList
{
private ArrayList _list = new ArrayList();
.....
}

You don't really need to do much for the implementation of the methods.
Simply add support for whatever functionality you want.

You'll probably want to implement an OnListChanged() method along these
lines:

protected void OnListChanged(ListChangedEventArgs args)
{
if (null != ListChanged)
{
ListChanged(this, args);
}
}
Since IBindingList inherits IList, ICollection, and IEnumerable, you'll have
to implement all of these as well. Simply ass them through to the underlying
ArrayList, such as:

public object this[int index]
{
get
{
return _list[index];
}
set
{
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged,
index, -1));
_list[index] = value;
}
}

public void RemoveAt(int index)
{
_list.RemoveAt(index);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, -1,
index));
}

public void Insert(int index, object value)
{
_list.Insert(index, value);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded,
index, -1));
}

public void Remove(object value)
{
int index = _list.IndexOf(value);
_list.Remove(value);
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, -1,
index));
}

and so on and so forth. Simply call OnListChanged with the appropriate
arguments for operations that change the collection.

Pete

<na********@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi Pete,

Thanks for the help but I will need some more help in using
IBindingList.
I will prefer this way but I could not get much help from MSDN
regarding the implementation of IBindlingList.

some more help regarding such implementation will be nice.

Thanks
Nad

Nov 18 '05 #4
Hi Pete,

Thank you so much for the tips. I have implemented the IBindingList
Interface successfully.
Actuallly I was not able to understand how my Class will notify the
CurrencyManager of DataGrid.

Thanks alot..

Nad

Nov 18 '05 #5
When you bind a class to the grid that implement IBindingList, the
CurrencyManager subscribes to the ListChanged event in the IBindingList. By
handling the ListChanged events, it can tell when items are added, removed,
etc, and then it can update its data to reflect those changes, allowing the
grid to stay in sync with the underlying data.
<na********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi Pete,

Thank you so much for the tips. I have implemented the IBindingList
Interface successfully.
Actuallly I was not able to understand how my Class will notify the
CurrencyManager of DataGrid.

Thanks alot..

Nad

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bernard Koninckx | last post by:
Hi everybody, The following code (putted in a inherited object from AbstractTableModel object) make some errors : public void deleteRow(int rowToDelete){ try{ Object dataObject =...
3
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
0
by: Stephen | last post by:
I have a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is...
2
by: Stephen | last post by:
I am trying to delete a row in a datagrid on the onclick of a asp:ButtonColumn. The datagrid is created from the items in an arraylist so what im trying to do is remove the item from the array and...
1
by: Harry | last post by:
I want to bind three elements of an seven element arraylist to a data grid. I cannot find an example of complex binding specific fields of an array list to a datagrid. Any examples would be...
16
by: stojilcoviz | last post by:
I've a datagrid whose datasource is an arraylist object. The arraylist holds many instances of a specific class. I've two questions about this: 1 - Is there a way by which I can obtain a...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
8
by: David | last post by:
Hi all, Using C# 1.1 I have a dataset loaded from an XML file. The idea being that if the app (a winform app that relies on a webservice) is offline, then I save data locally in XML and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.