Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one combobox with flowers,fruits,vegetables
when i select combobox1 in combobox2 it has to display types of fruits(apple,mango,orange)..all this has to be done dynamically means getting the values from sql server

I worked with this code it's not working...it's showing error at
MaskId = Convert.ToInt32(cmbmoduleid.Text);
----------------------------------------------------------------------
C#
private void cmbmoduleid_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbmoduleid.SelectedIndex > -1 && cmbmoduleid.SelectedItem != null)
            {
        
                if (cmbmoduleid.SelectedText.ToString() != "")
                {
                   MaskId = Convert.ToInt32(cmbmoduleid.Text);
                    DatasetForm = glbObj.srv.ViewDataSRV(clsGetViewXmlData.GetViewXMLData(gInfo.SchemaName, GlobalSP.FormInsert.Usp_FormInsert_SelectSubModule.ToString(), MaskId));
                    DataTable Dt = DatasetForm.Tables[0];

                    //cmbmoduleid.DataSource = new BindingSource(ImpecApplication.Cust.Reports.Global.GLB_FORMS, null);
                    cmbsubmoduleid.DataSource = Dt;
                    cmbsubmoduleid.DisplayMember = "SubModule_Name"; //colum you want to show in comboBox
                    cmbsubmoduleid.ValueMember = "SubModule_ID";
                }
            }

        }
Posted
Updated 25-Jun-12 1:03am
v2
Comments
Ganesan Senthilvel 25-Jun-12 7:03am    
Code format
[no name] 25-Jun-12 7:34am    
You would be better off using TryParse to convert strings to numeric values.

1 solution

C#
public Form1()
{
    InitializeComponent();
    List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
    list.Add(new KeyValuePair<string, int>("apple", 1));
    list.Add(new KeyValuePair<string, int>("mango", 2));
    list.Add(new KeyValuePair<string, int>("orange", 3));
    comboBox1.DisplayMember = "Key";
    comboBox1.ValueMember = "Value";
    comboBox1.DataSource = list;

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int selectedValue = (int)comboBox1.SelectedValue;

}


Or you can use your custom class to bind as combobox data source

C#
public class ComboItem
{
    public string stringValue { get; set; }
    public int indexValue { get; set; }
}

public void LoadCombo()
{
     List<ComboItem> list = new List<ComboItem>();
     // populate list...
     // then bind list
     myComboBox.DisplayMember = "stringValue";
     myComboBox.ValueMember = "indexValue";
     myComboBox.DataSource = list;
}
Then

myComboBox.SelectedText       // will return stringValue
myComboBox.SelectedValue      // will return indexValue
myComboBox.SelectedItem       // will return the ComboItem itself
myComboBox.SelectedIndex      // will return the items index in the list



http://stackoverflow.com/questions/4691731/winforms-c-adding-items-to-combox-and-controlling-the-item-value-numeric[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900