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