an error of setting comboBox.SelectedValue

  • Thread starter Thread starter chen qi
  • Start date Start date
C

chen qi

## i have a table like this:

myID myName
------------------------ -----------------------------
1 aaa
2 bbb
3 ccc

## in VS2003, i write code:
//
// step 1: create a dataset, and load data in table1
//
DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(...);
da.Fill(ds, "table");
//
// step 2: add a new datarow at the top of table1
//
DataRow dr = ds.Tables["table1"].NewRow();
dr["myID"] = "0";
dr["myName"] = "<all>";
ds.Tables["table1"].Rows.InsertAt(dr, 0);
ds.Tables["table1"].AcceptChanges();
//
// step 3: bind comboBox1
//
comboBox1.DataSource = ds.Tables["table1"];
comboBox1.DisplayMember = "myName";
comboBox1.ValueMember = "myID";

## it works all right. but when i run following code, an error displayed.

comboBox1.SelectedValue = 1; //error(System.ArgumentOutOfRangeException)

## i modified the code(1->2), no error displayed.

comboBox1.SelectedValue = 2; //no error

## but after removing the step 2, no error occured when i run
"comboBox1.SelectedValue = 1;" .
## i was confused, please help me. thank you in advance.
 
hi chen
do you have the value one in your table ??, if so then this is very
strange behavior , there must be something like order of initialization of
object or something . Would you post a snippet of your code.
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Back
Top