C#-APP: Problems dynamically databinding comboboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theleshie
    New Member
    • May 2008
    • 3

    C#-APP: Problems dynamically databinding comboboxes

    Hi,
    I am creating an application which dynamically creates forms depending on the information held in a dataset (which also includes the data the application itself uses). As part of dynamically creating these forms, I need to bind controls back to their underlying tables. This has been successful with textboxes, but I am having a lot of difficulty with comboboxes. Setting the datasource to the bindinglist seems to be ok, but I am having trouble setting the selectedindex/item/value (I believe it is item I need to use to bind to the underlying object) when I initialise the form, FindStringExact doesn't seem to be able to find the item which throws an exception.

    Here is the part of the code which generates comboboxes:

    //datarowview for the particular piece of information
    DataRowView drv_dataRow = ((DynamicCharac teristic)cashfl owchars[i]).getDataRow();
    //instantiate new combobox
    ComboBox cboBox_char = new ComboBox();
    //set name
    cboBox_char.Nam e = ((DynamicCharac teristic)cashfl owchars[i]).getCharID().T oString();
    //get options for combobox
    ArrayList options = ((DynamicCharac teristic)cashfl owchars[i]).getOptions();

    //create bindinglist from array
    BindingList<PDR eportDataSet.Ch aracteristicLis tItemRow> bl = new BindingList<PDR eportDataSet.Ch aracteristicLis tItemRow>();

    //populte bindinglist
    for (int j = 0; j < options.Count; j++)
    {
    bl.Add((PDRepor tDataSet.Charac teristicListIte mRow)options[j]);
    }

    //set members and datasource
    cboBox_char.Dis playMember = "ShortName" ;
    cboBox_char.Val ueMember = "Characteristic ListItemID";
    cboBox_char.Dat aSource = bl;

    //set databinding
    cboBox_char.Dat aBindings.Add(" SelectedValue", drv_dataRow, "Characteristic Value");

    //set current item in combobox
    int index = cboBox_char.Fin dStringExact(xm lRenderingClass .getListItemNam e(Int32.Parse(( (DynamicCharact eristic)cashflo wchars[i]).getVal())));
    cboBox_char.Sel ectedIndex = index;
    //add event handling
    cboBox_char.Sel ectedIndexChang ed += new System.EventHan dler(this.chara cteristicComboB ox_SelectedInde xChanged);
    //format combobox
    cboBox_char.Dro pDownStyle = ComboBoxStyle.D ropDownList;
    cboBox_char.Wid th = 200;
    //add control to tablelayoutpane l
    tablepanel.Cont rols.Add(cboBox _char, 1, i);


    Any help would be much appreciated!
Working...