473,396 Members | 1,940 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,396 software developers and data experts.

BindingList and ArgumentOutOfRange when bound to DataGridView

Hi,

When I bind a BindingList<to a DataGridView and then call RemoveAt(n) on
it, I get ArgumentOutOfRange. Could someone explain why?

Sample:

public partial class TestForm2 : Form
{
private BindingList<Person_personList = new BindingList<Person>();

public TestForm2()
{
InitializeComponent();
}

private void TestForm2_Load(object sender, EventArgs e)
{
Person personA = new Person("Pete");
Person personB = new Person("James");
_personList.Add(personA);
_personList.Add(personB);

uxTestGrid.DataSource = _personList;
}

private void uxTestButton_Click(object sender, EventArgs e)
{
_personList.RemoveAt(0);
}
}

public class Person
{
public string Name = "";
public Person(string name)
{
Name = name;
}
}

So I have two Person objects in the BindingList. Yet, when I call
_personList.RemoveAt(0) (or 1), I get this exception. If I do not bind it
to the DataGridView, it works fine.

Please help.

Thanks,
Jim
Dec 20 '07 #1
1 2092
Hi Jim,

I'm not exactly sure of this, but I think because Person does not have any
properties it is not visible in the DataGridView, and internally the
DataGridView operates on an empty Person-list. When the datasource removes
one item the DataGridView does not have any items to remove.

If you add a property to Person, it should work fine.

public class Person
{
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

public Person(string name)
{
Name = name;
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"Jim Balo" wrote:
Hi,

When I bind a BindingList<to a DataGridView and then call RemoveAt(n) on
it, I get ArgumentOutOfRange. Could someone explain why?

Sample:

public partial class TestForm2 : Form
{
private BindingList<Person_personList = new BindingList<Person>();

public TestForm2()
{
InitializeComponent();
}

private void TestForm2_Load(object sender, EventArgs e)
{
Person personA = new Person("Pete");
Person personB = new Person("James");
_personList.Add(personA);
_personList.Add(personB);

uxTestGrid.DataSource = _personList;
}

private void uxTestButton_Click(object sender, EventArgs e)
{
_personList.RemoveAt(0);
}
}

public class Person
{
public string Name = "";
public Person(string name)
{
Name = name;
}
}

So I have two Person objects in the BindingList. Yet, when I call
_personList.RemoveAt(0) (or 1), I get this exception. If I do not bind it
to the DataGridView, it works fine.

Please help.

Thanks,
Jim
Dec 20 '07 #2

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

Similar topics

3
by: guy | last post by:
VB2005 a generic collection that inherits from BindingList, with the code to support sorting added (ApplySortCore, SpportsSortingCore etc.) however when this collection is populated and bound...
1
by: Pieter | last post by:
Hi, I have a custom List that inherits from BindingList. It has some methods overloaded, like the Add/Insert/etc to add and remove some eventhandlers when adding or removing an item T of the...
5
by: Mike Surcouf | last post by:
Hi All I have a stored procedure wrapper that returns Collection<T>. The wrapper is generated by codesmith so I don't really want to start altering it. I need to use this collection in a...
6
by: jwilson128 | last post by:
I am trying to decide whether to use a system.ComponentModel.BindingList(of T) or a Sytem.Collections.Generic.List(of T) for a custom collection. In testing a simple, read-only data binding to a...
1
by: Rick | last post by:
VS.net 2005 I'm using a bindinglist(of T) to bind to various controls on a WinForm. I use the name/value pairs in the "type" for the displayMember and ValueMember of say a ComboBox control. ...
10
by: Rick | last post by:
VS 2005 I have a class of BindingList(of T) to use as a datasource for a lookup combobox. The combobox is to lookup the Terms for a Purchase Order. When I implement this on my Winform, the...
0
by: =?Utf-8?B?Q2xhdWRl?= | last post by:
It is possible to capture the "AddingNew" event to create our own object before it is append to the BindingList (using e.NewObject = new ...) When the user press delete on a row to remove a row...
3
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this...
0
by: myotheraccount | last post by:
I'm a newbie to c#, so bear with me. I'm trying to get data into from a DataReader, put it into a BindingList that is bound into a DataGridView. My code is: BindingList<stringbl = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.