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

IndexOutOfRangeException with BindingContext

I have a datagrid bound to a strongly typed custom collection. I have
a Delete button on my form that calls the following code:

if ( this.BindingContext[myDataSource].Count > 0 )
{
myDataSource.RemoveAt( this.BindingContext[myDataSource].Position
);
}

I was expecting the button press to remove the elevant row from the
Datagrid, but I get an IndexOutOfRangeException saying No value at
index 2.
If I add the following below the RemoveAt call, everything works
correctly

this.dataGrid1.DataSource = null;
this.dataGrid1.DataSource = myDataSource;

I should not have to rebind my datagrid to my datasource. Can anyone
tell me what I have done wrong?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
1 2123
Stephen,

Does your custom collection implement the IBindingList interface? I
assume it doesn't. When it implements this interface, it implements a
ListChanged event which indicates to whatever is binding to your collection
that the collection itself has changed. If you didn't implement this, then
you would have to always rebind to your collection, because the grid has no
clue when the list has changed (it only knows what it read from the list on
hookup).

Hope this helps.
"Stephen.Haeney" <st************@tellermate-dot-com.no-spam.invalid> wrote
in message news:40********@Usenet.com...
I have a datagrid bound to a strongly typed custom collection. I have
a Delete button on my form that calls the following code:

if ( this.BindingContext[myDataSource].Count > 0 )
{
myDataSource.RemoveAt( this.BindingContext[myDataSource].Position
);
}

I was expecting the button press to remove the elevant row from the
Datagrid, but I get an IndexOutOfRangeException saying No value at
index 2.
If I add the following below the RemoveAt call, everything works
correctly

this.dataGrid1.DataSource = null;
this.dataGrid1.DataSource = myDataSource;

I should not have to rebind my datagrid to my datasource. Can anyone
tell me what I have done wrong?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2

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

Similar topics

9
by: Tylius | last post by:
This one line is causing the issue, I've searched all over the net, but I can't seem to figure out why public static void Main(string args) { Console.WriteLine("Dice Roller"); try { int...
1
by: D. Yates | last post by:
Hi, I retrieved the employee table from the Pubs database into a single dataset called, dataSet12. I dropped two textbox controls and a datagrid control onto the same form and bound the...
0
by: scotthutchinson | last post by:
I have a .NET Remoting object hosted in IIS6 on Windows Server 2003 (happens before and after installing SP1) at an endpoint (ASP.NET application virtual folder) named "CompanyXYZReporting". The...
0
by: Nathan Carroll | last post by:
Attempting to eliminate flicker showing (form) and hiding seems to have no effect as I have it below. if I move the lines: Me.Show() Me.ShowInTaskbar = True f.Close() to the end of the...
1
by: Edwin | last post by:
Hi, I created a dataform with the data form wizard which created some textboxes and 1 checkbox. When I add a row (see section "Add Row" for code) the newly added is not accessible. A label...
0
by: Donald | last post by:
Mostly, I get the currencymanager from the form.bindingcontext property. but I found that I can aslo get the same currencymanager from a textbox.bindingcontext, even though this textbox does not...
2
by: | last post by:
Using the following against a sql database, I get a system.IndexOutOfRangeException for the column name Add_Date which is valid field name and is a datetime field. All the other columns work ok. ...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
0
by: =?Utf-8?B?aW1yYW4uYQ==?= | last post by:
Hi all, I am having a problem with inserting rows in to my datagridview control. The datagridview is bound to a bindingsource with a filter set. If i try and add a row to the datagridview while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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,...

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.