Answered task -creating dropdownlist boxs and accessing them in c#

  • jueves, 03 de mayo de 2012 12:29
     
     

    create two dropdown listbox called "names" and "id" .the names dropdownlist consist of names like jony,zen,mortin   and id drop downlist consist of id 1,4,5 ....task is when one select a name from name dropdownlist the corresponding id for that name should be visible in id dropdownlist..pls provide code in c#


    sel.........

Todas las respuestas

  • jueves, 03 de mayo de 2012 14:53
     
     Respondida Tiene código

    Hi, 

    Is names and corresponding Id's are in single entity?

      class Item
      {
                public int Id { get; set; }
                public string Name { get; set; }
      }


    You can bind this to comboBox as, 

    //set the data source for the combobox

    //and set Display and value

    comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "Id";

    //fill ComboBox2 datasource with display member as ID's

    and

       comboBox1.SelectedIndexChanged += OnSelectedIndexChanged;

    on event handler

    void OnSelectedIndexChanged(object sender, EventArgs e)
    {
     comboBox2.SelectedIndex = comboBox2.Items.IndexOf(((Item)comboBox1.SelectedItem).Id);    
    }

    Hope this helps you...

    BTW, check this post for choosing right forum.


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • viernes, 04 de mayo de 2012 13:43
     
     Respuesta propuesta Tiene código

    Hi sa5000174,

    On OnSelectedIndexChanged of combo1, you can do this:

    void OnSelectedIndexChanged(object sender, EventArgs e)
    {
     comboBox2.SelectedIndex = comboBox2.Items.IndexOf(((Item)comboBox1.SelectedItem).Id);    
    }


    Regards, http://shwetamannjain.blogspot.com

    • Propuesto como respuesta Shweta Jain viernes, 04 de mayo de 2012 13:43
    •