472,126 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

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 = ((DynamicCharacteristic)cashflowchars[i]).getDataRow();
//instantiate new combobox
ComboBox cboBox_char = new ComboBox();
//set name
cboBox_char.Name = ((DynamicCharacteristic)cashflowchars[i]).getCharID().ToString();
//get options for combobox
ArrayList options = ((DynamicCharacteristic)cashflowchars[i]).getOptions();

//create bindinglist from array
BindingList<PDReportDataSet.CharacteristicListItem Row> bl = new BindingList<PDReportDataSet.CharacteristicListItem Row>();

//populte bindinglist
for (int j = 0; j < options.Count; j++)
{
bl.Add((PDReportDataSet.CharacteristicListItemRow) options[j]);
}

//set members and datasource
cboBox_char.DisplayMember = "ShortName";
cboBox_char.ValueMember = "CharacteristicListItemID";
cboBox_char.DataSource = bl;

//set databinding
cboBox_char.DataBindings.Add("SelectedValue", drv_dataRow, "CharacteristicValue");

//set current item in combobox
int index = cboBox_char.FindStringExact(xmlRenderingClass.getL istItemName(Int32.Parse(((DynamicCharacteristic)ca shflowchars[i]).getVal())));
cboBox_char.SelectedIndex = index;
//add event handling
cboBox_char.SelectedIndexChanged += new System.EventHandler(this.characteristicComboBox_Se lectedIndexChanged);
//format combobox
cboBox_char.DropDownStyle = ComboBoxStyle.DropDownList;
cboBox_char.Width = 200;
//add control to tablelayoutpanel
tablepanel.Controls.Add(cboBox_char, 1, i);


Any help would be much appreciated!
May 8 '08 #1
0 1218

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Musravus | last post: by
6 posts views Thread by Papa.Coen | last post: by
reply views Thread by Evan | last post: by
1 post views Thread by esakal | last post: by
3 posts views Thread by Mark Denardo | last post: by
3 posts views Thread by =?Utf-8?B?VmFuZXNzYQ==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.