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

A

Andrus

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());
}
}
 
K

Kevin Spencer

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
 
A

Andrus

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.
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());
}
}
 
?

=?windows-1257?Q?G=F6ran_Andersson?=

Andrus said:
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());
}
}

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.
 
?

=?windows-1257?Q?G=F6ran_Andersson?=

Andrus said:
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.
 
T

Tom Spink

Andrus said:
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());
}
}

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();
 
F

Family Tree Mike

Rachit said:
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.
 

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