Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Value of '0' is not valid for 'SelectedIndex'

889 views
Skip to first unread message

Andrus

unread,
Jun 22, 2007, 9:47:50 AM6/22/07
to
To reproduce, run the code.

Observed:
System.ArgumentOutOfRangeException was unhandled
Message="InvalidArgument=Value of '0' is not valid for
'SelectedIndex'.\r\nParameter name: SelectedIndex"

t2.c1 contains legal value.
Why this error occurs ?
How to fix ?

Andrus.


using System.Windows.Forms;
using System.Data;
using System;
class testForm : Form {

testForm() {
DataTable t = new DataTable();
t.Columns.Add("displaymember");
t.Columns.Add("valuemember");
t.Rows.Add("lower", "l");

ComboBox comboBox1 = new ComboBox();
comboBox1.DisplayMember = "displaymember";
comboBox1.ValueMember = "valuemember";
comboBox1.DataSource = t;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

DataTable t2 = new DataTable();
t2.Columns.Add("c1");
t2.Rows.Add("l");
comboBox1.DataBindings.Add("SelectedValue", t2, "c1");
Controls.AddRange(new Control[] { comboBox1 });
}

[STAThread]
static void Main() {
Application.Run(new testForm());
}
}

Kevin Spencer

unread,
Jun 22, 2007, 11:05:50 AM6/22/07
to
It isn't legal if it's a string.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Andrus" <kobru...@hot.ee> wrote in message
news:eMNs7PNt...@TK2MSFTNGP05.phx.gbl...

Andrus

unread,
Jun 22, 2007, 11:55:05 AM6/22/07
to
Kevin,

thank you.
The code does not set SelectedIndex as all.

Why .NET tries to set SelectedIndex to invalid value ?

Andrus.

> It isn't legal if it's a string.
>>

Göran Andersson

unread,
Jun 22, 2007, 12:41:02 PM6/22/07
to

You can't select a specific item from a combobox that doesn't contain
any items at all.

You haven't databound the combobox, so it doesn't contain any items.

--
Göran Andersson
_____
http://www.guffa.com

Göran Andersson

unread,
Jun 22, 2007, 12:46:17 PM6/22/07
to
Andrus wrote:
> Kevin,
>
> thank you.
> The code does not set SelectedIndex as all.
>
> Why .NET tries to set SelectedIndex to invalid value ?
>
> Andrus.

You are setting the SelectedValue property. That will basically look for
the value among the items, and set the SelectedIndex property for the
matching item.

Tom Spink

unread,
Jun 22, 2007, 1:14:24 PM6/22/07
to
Andrus wrote:

Hi,

This is *totally* a guess, because I haven't tried your code, but try moving
this line:

comboBox1.DataSource = t;

So it immediately follows this line:

ComboBox comboBox1 = new ComboBox();

--
Tom Spink
University of Edinburgh

Rachit verma

unread,
Sep 13, 2009, 6:06:56 AM9/13/09
to
Unhandled Exception:

InvalidArgument=Value of '41' is not valid for 'SelectedIndex'.
Parameter name: SelectedIndex

System.ArgumentOutOfRangeException

Stack Trace:
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at SpeedConnect7VB.SpeedConnect.Display_Defaults()
at SpeedConnect7VB.SpeedConnect.SpeedConnect_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.Show()
at SpeedConnect.FormOptimizer.ReloadOptimizer()
at SpeedConnect.FormOptimizer.FormOptimizer_VisibleChanged(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

From http://www.google.co.in/search?client=firefox-a&rls=org.mozilla:en-US:official&channel=s&hl=en&source=hp&q=+InvalidArgument=Value+of+'41'+is+not+valid+for+'SelectedIndex'.&meta=&btnG=Google+Search

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Family Tree Mike

unread,
Sep 13, 2009, 7:28:53 AM9/13/09
to
Rachit verma wrote:
> Unhandled Exception:
>
> InvalidArgument=Value of '41' is not valid for 'SelectedIndex'.
> Parameter name: SelectedIndex
>
> System.ArgumentOutOfRangeException

I'm not sure why you responded to a post from several years ago...

At the line where you get the error, how many items does the debugger
say are in the control? If there are 41 items, you must use
SelectedIndex in the range 0 to 40.

--
Mike

0 new messages