Hi,
I am using this code to populate data into combobox but i was just thinking how can i assign an id to each item in the dropdown list like the primary id for example because i want to save both id and name of the company..
// fill the insurance company;
VDBAdapter.SelectCommand =
new VistaDBCommand("SELECT account_name AS insurance_account FROM accounts WHERE account_type = 'INSURANCE' ORDER BY account_name", VDBConnection);VDBDataset.Tables.Add("insurance_account");
VDBAdapter.Fill(VDBDataset, "insurance_account");
//put the data from the dataset into the comboboxDataTable tblInsuranceCompany = VDBDataset.Tables["insurance_account"];
if (oDataTable == null) throw new Exception("BoxCustomers table not found in oCustomersDataSet."); foreach (DataRow oRow in tblInsuranceCompany.Rows){
cboInsurance.Items.Add(oRow["insurance_account"]);
}

dropdown item ID
xpach
In addition to what has been said, you cannot set the values stored in the SelectedValue per row manually. Your choices are to create a class and place it in the arraylist and set the list as the datasource and set the DisplayMember and ValueMember to the respective properties of the class or you can do what cgraus has suggested earlier.
If you manually add rows, there is no way that you can set values in your ValueMember.
cheers,
Paul June A. Domag
CodePrince
Hi,
If you want to have a blank string being displayed in your combobox then you can manually set the selectedindex property to -1. But I guess you want to place this in the list. Could you try adding the blank list or the "Please choose counter..." item in the list at design time Try if the defaultly added string would still be in the list even after binding the combo.
To the Databindings.Add() this method specifies which field of your control is being binded to a datasource. For example you have a bit field, then you want that bit field to change a textbox's enabled property whenever the field if 0. This is the scenario in which you can use Databindings.Add()...
cheers,
Paul June A. Domag
*ferhat
callmekv
Sometimes I need an empty entry or one like "Please choose country...". The way I solved it for now is manually add an empty row to my DataSource in advance:
DataRow drEmptyEntry = dtMyData.NewRow();
drEmptyEntry["id_field"] = int.MinValue;
drEmptyEntry["text"] = string.Empty; // or "Please choose country..."
dtSolution.Rows.InsertAt(drEmptyEntry, 0); // inserts the empty entry as the first row
Then I set the DataSource to my DataTable (dtMyData) and the ValueMember and DisplayMember accordingly. How do you guys do this I have the feeling there's gotta be a better way.
The other issue with the DataSource is I couldn't yet quite figure out what exactly the DataBindings.Add does. It works fine without and seems messy with, but the sorting works neither way (ComboBox, ListView, ...) when I just set the Sorted property in the design mode.
Kajal Sinha
Hi there.. is this wat ur looking for
foreach (DataRow oRow in tblInsuranceCompany.Rows)
{
cboInsurance.Items.Add(oRow["insurance_account"]);
cboInsurance.DataTextFiled = oRow["DisplayColumnName"].ToString();
cboInsurance.DataValueFiled = oRow["KeyColumn"];
}
lukeliu
rblaco
bagira20572
I have experience the same issue. I did resolve it using a class that populates an array and the use the class for the datasource.
The reason I needed to manually add the items is because the dataset had the first and last name fields in separate fields and I need the listbox to display them as lastname, firstnace and I need to set the selectedValue of the item to the ID of the person for later retrieval on a save.
Yes, you can do this with the array concept, however, when trying to use RemoveAt on a listbox that has a datasource bound to it; won't work. Therefore, I had to set the listboxes datasource to nothing and use RemoveAt on the class and then reassign the datasource to the listbox. This painfully slow.
It would have been nice to have the Add let you set the display and value for the item.
Syed Junaid
but i am not using the DisplayMember