ArgumentOutOfRange on set ComboBox.SelectedIndex

G

Guest

Hi,

I'm getting an ArgumentOutOfRange being thrown from a ComboBox that has a
DataBinding on SelectedValue to a public property on a class. The DataSource
of the ComboBox is a DataTable that has had a row inserted into it (via
DataTable.Rows.Insert( row, index ) ). After a row has been inserted, I can
no longer bind. If I append a new row using the Add method instead of
inserting it, the table binds fine.

Is it not possible to bind with a DataTable that has had rows inserted?
Seems to me the index its returning is incorrect, considering I said insert
at 0, and its saying -2147483642...

Any enlightment is greatly appreciated. The error details and actual stack
trace are below, along with the sample code to reproduce the error. This is
on framework v1.1, btw.

Thanks,
Nathan Ernst

The error information I get is:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: Specified argument was out of the range
of valid values.
Parameter name: '-2147483642' is not a valid value for 'index'.
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object
component, Object value)
at System.Windows.Forms.Binding.SetPropValue(Object value)
at System.Windows.Forms.Binding.PushData()
at System.Windows.Forms.Binding.UpdateIsBinding()
at System.Windows.Forms.Binding.CheckBinding()
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding
dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext
newBindingContext, Binding binding)
at System.Windows.Forms.Control.UpdateBindings()
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.ListControl.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
ComboBoxSelection
Assembly Version: 1.0.1903.28155
Win32 Version: 1.0.1903.28155
CodeBase:
file:///C:/TestProjects/ComboBoxSelection/ComboBoxSelection/bin/Debug/ComboBoxSelection.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Accessibility
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.


If I run the application in the debugger and break on the exception, here is
the true stack trace (Note the exception actually occurs from
set_SelectedIndex on ComboBox):

system.windows.forms.dll!System.Windows.Forms.ComboBox.set_SelectedIndex(int
value) + 0x175 bytes

system.windows.forms.dll!System.Windows.Forms.ListControl.set_SelectedValue(System.Object value) + 0x77 bytes
mscorwks.dll!_CallDescrWorker@16() + 0x1c
mscorwks.dll!COMMember::InvokeMethod() + 0x95a

system.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(System.Object component, System.Object value) + 0x139 bytes

system.windows.forms.dll!System.Windows.Forms.Binding.SetPropValue(System.Object value) + 0xf6 bytes
system.windows.forms.dll!System.Windows.Forms.Binding.PushData() + 0x28
bytes
system.windows.forms.dll!System.Windows.Forms.Binding.UpdateIsBinding() +
0x59 bytes
system.windows.forms.dll!System.Windows.Forms.Binding.CheckBinding() +
0x310 bytes

system.windows.forms.dll!System.Windows.Forms.ListManagerBindingsCollection.AddCore(System.Windows.Forms.Binding dataBinding) + 0x3b bytes

system.windows.forms.dll!System.Windows.Forms.BindingsCollection.Add(System.Windows.Forms.Binding binding) + 0x10 bytes

system.windows.forms.dll!System.Windows.Forms.BindingContext.UpdateBinding(System.Windows.Forms.BindingContext
newBindingContext, System.Windows.Forms.Binding binding) + 0xde bytes
system.windows.forms.dll!System.Windows.Forms.Control.UpdateBindings() +
0x51 bytes

system.windows.forms.dll!System.Windows.Forms.Control.OnBindingContextChanged(System.EventArgs e) + 0x2b bytes

system.windows.forms.dll!System.Windows.Forms.ListControl.OnBindingContextChanged(System.EventArgs e) + 0x2b bytes
system.windows.forms.dll!System.Windows.Forms.Control.CreateControl() +
0x49 bytes

system.windows.forms.dll!System.Windows.Forms.Control.OnVisibleChanged(System.EventArgs e) + 0x43 bytes

system.windows.forms.dll!System.Windows.Forms.Control.WmShowWindow(System.Windows.Forms.Message m) + 0x1b7 bytes

system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message m) + 0x641 bytes

system.windows.forms.dll!System.Windows.Forms.ComboBox.WndProc(System.Windows.Forms.Message m) + 0x418 bytes

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message m) + 0xb bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message m) + 0xbc bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int hWnd, int msg, int wparam, int lparam) + 0x27 bytes
user32.dll!_InternalCallWinProc@20() + 0x28
user32.dll!_UserCallWinProcCheckWow@32() + 0xb7
user32.dll!_DispatchClientMessage@20() + 0x4d
user32.dll!___fnDWORD@4() + 0x24
ntdll.dll!_KiUserCallbackDispatcher@12() + 0x13
system.windows.forms.dll!System.Windows.Forms.Control.SetParentHandle(int
value) + 0x23a bytes
system.windows.forms.dll!System.Windows.Forms.Control.CreateControl(bool
fIgnoreVisible) + 0x131 bytes
system.windows.forms.dll!System.Windows.Forms.Control.CreateControl() +
0xd bytes

system.windows.forms.dll!System.Windows.Forms.Control.WmShowWindow(System.Windows.Forms.Message m) + 0x9a bytes

system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message m) + 0x641 bytes

system.windows.forms.dll!System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message m) + 0x36 bytes

system.windows.forms.dll!System.Windows.Forms.ContainerControl.WndProc(System.Windows.Forms.Message m) + 0x17 bytes

system.windows.forms.dll!System.Windows.Forms.Form.WmShowWindow(System.Windows.Forms.Message m) + 0x36 bytes

system.windows.forms.dll!System.Windows.Forms.Form.WndProc(System.Windows.Forms.Message m) + 0x1f5 bytes

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message m) + 0xb bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message m) + 0xbc bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int hWnd, int msg, int wparam, int lparam) + 0x27 bytes
user32.dll!_InternalCallWinProc@20() + 0x28
user32.dll!_UserCallWinProcCheckWow@32() + 0xb7
user32.dll!_DispatchClientMessage@20() + 0x4d
user32.dll!___fnDWORD@4() + 0x24
ntdll.dll!_KiUserCallbackDispatcher@12() + 0x13
system.windows.forms.dll!System.Windows.Forms.Control.SetVisibleCore(bool
value) + 0xfa bytes
system.windows.forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool
value) + 0xec bytes
system.windows.forms.dll!System.Windows.Forms.Control.set_Visible(bool
value) + 0xe bytes
system.windows.forms.dll!ThreadContext.RunMessageLoopInner(int reason,
System.Windows.Forms.ApplicationContext context) + 0xc2 bytes
system.windows.forms.dll!ThreadContext.RunMessageLoop(int reason,
System.Windows.Forms.ApplicationContext context) + 0x45 bytes

system.windows.forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x34 bytes
ComboBoxSelection.exe!ComboBoxSelection.Form1.Main() Line 183 C#




Here is some sample code to reproduce the exception:
/// <code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ComboBoxSelection
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public class DataObject
{
#region Name Property
private string _Name = string.Empty;
public string Name
{
get{ return this._Name; }
set
{
if ( value == null )
value = string.Empty;

if ( value == this._Name )
return;

this._Name = value;
this.OnNameChanged( EventArgs.Empty );
}
}

#region NameChanged Event
public event EventHandler NameChanged;

protected void OnNameChanged( EventArgs arguments )
{
EventHandler handler = this.NameChanged;
if (handler != null )
handler( this, arguments );
}
#endregion
#endregion

#region ID Property
private string _ID = string.Empty;
public string ID
{
get{ return this._ID; }
set
{
if ( value == null )
value = string.Empty;

if ( value == this._ID )
return;

this._ID = value;
this.OnIDChanged( EventArgs.Empty );
}
}

#region IDChanged Event
public event EventHandler IDChanged;

protected void OnIDChanged( EventArgs arguments )
{
EventHandler handler = this.IDChanged;
if (handler != null )
handler( this, arguments );
}
#endregion
#endregion

public DataObject()
{
}
}

private DataTable GetData()
{
DataTable table = new DataTable( "TestData" );

table.Columns.Add( "Name" );
table.Columns.Add( "ID" );

// Create some data

table.Rows.Add( new object[]{ "John", "1" } );
table.Rows.Add( new object[]{ "Joe", "2" } );
table.Rows.Add( new object[]{ "Jim", "3" } );
table.Rows.Add( new object[]{ "Jan", "4" } );
table.Rows.Add( new object[]{ "Jane", "5" } );

return table;
}

private System.Windows.Forms.ComboBox comboTest;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

DataTable table = this.GetData();
DataRow row = table.NewRow();
row["Name"] = "<none>";
row["ID"] = string.Empty;

table.Rows.InsertAt( row, 0 );

this.comboTest.DataSource = table;
this.comboTest.ValueMember = "ID";
this.comboTest.DisplayMember = "Name";

// Setup some data binding
this.comboTest.DataBindings.Add( "SelectedValue", new DataObject(), "ID" );
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
this.comboTest.DataBindings.Clear();

if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboTest = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboTest
//
this.comboTest.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboTest.Location = new System.Drawing.Point(8, 8);
this.comboTest.Name = "comboTest";
this.comboTest.Size = new System.Drawing.Size(280, 21);
this.comboTest.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 29);
this.Controls.Add(this.comboTest);
this.Name = "Form1";
this.Text = "ComboBox SelectedIndex error";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
/// </code>
 
G

Guest

On a whim, I tried the code on .Net 2.0 Beta 1, and the Code works fine. I
don't get the ordering in the combobox I'd hoped for, but it does not throw
while data binding.

-Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top