Forms issue with combobox

  • Thread starter Thread starter David
  • Start date Start date
D

David

In C#, I am creating a combobox and loading the data in via. the
DataSource
method.

Eg:
this.comboBox.DataSource = a1;
this.comboBox.DisplayMember = "name";
this.comboBox.ValueMember = "key";

I also need to set the displayed item in the combobox before the form
is
displayed to the user. The issue is that when I attempt to set the
select
index, etc. prior to showing the form, I get a error:

System.ArgumentOutOfRangeException: Specified argument was out of the
range
of valid values.
Parameter name: '2' is not a valid value for 'index'.

However, when I perform the same command after the form is loaded, ex.
via.
a button, it works. I have to assume that something on the form is
not set
prior to final form loading.

So, where can I place my code so that it will run after the datasource
pointers are set up and prior to the user getting their hands on the
form?
Eg. should I put it in a event? I tried the form load, but also too
early.

Or is there another way to approach this issue.
 
make sure your datasource 'a1' is filled up before you place code
selectedindex = <value>

or

'a1' contain less number of rows then <value> you try to set.

I assume, that would be the problem you are facing.

Rajesh Patel
 
Back
Top