c# - I can not set a value for ComboBox.SelectedIndex or any other selected -


my name's arnaldo, i'm brazil , need study c#.

i wanna create real model or real case study.

i use poo, polymorphism , heritage.

i've "frmbase", have need single form, db connection, change size , more.

the next one, instantiates "frmbase" "frmbaseconsulta", second have "tabcontrol" 2 guides, 1 gridview , other register information.

and finally, next one, instantiates "frmbaseconsulta" "frmcadpescity"; put in form textbox called "txbcityname" , combobox called "cmbstate".

the combobox populated table contains record of states, follows:

    // script in form "frmcadpescity"     public void getstates()                                   //      {         bindingsource dbstates = new bindingsource();          dbstates = dal.openquery("select stateid, " +                                  "       stateabbreviation || ' - ' ||statename statename  "+                                  "  country.tbstates  " +                                  " order stateabbreviation");          cmbstate.valuemember = "stateid";         cmbstate.displaymember = "statename";                     cmbstate.datasource = dbstates;     }      // "childresources" abstract method , declared in "frmbaseconsulta"     // , override when necessary, thi, in "frmcadpescity"     public override void childresources()     {         //          getstates();     } 

in datagridview select record ["sam diego", "ca"] correct ("san diego") , click in update button, guide change "register", in "txbcityname" object receive "sam diego", "cmbstate" stay in te first record "ak".

follows script change selectedindex value of combobox :

            // method , declared in "frmbaseconsulta"             ...             else if (focusedelement combobox)             {                 // value in table based on object name                 // getoldeditorvalue => "ca - california"                 string seditorvalue = getoldeditorvalue(((combobox)focusedelement).name);                 int iindexvalue = -1;                  // find indexvalue                 iindexvalue = ((combobox)focusedelement).findstringexact(seditorvalue);                 ((combobox)focusedelement).selectedindex = iindexvalue;             }             ... 

i tried possible alternatives , not work , can me this?

selectedindex selectedvalue selectedtext selecteditem

one more thing , when create textbox programmatically see if picking data, textbox created correctly. follows code:

                textbox txtcomboboxresult = new textbox();                 txtcomboboxresult.text = seditorvalue + " --> " + iindexvalue.tostring();                 pnlcadastro.controls.add(txtcomboboxresult); 

please, need help...

sorry bad english...


Comments