locked
selecting an item in combobox and display corresponding id in textbox??? RRS feed

Answers

  • ASP.NET Code
    -----------------

    cboGender.Items.Add(new ListItem("Male", "M"));
    cboGender.Items.Add(new ListItem("Female", "F"));

    On the selection index change event of combo box
    ----------------------------------------------------------

    Textbox1.Text = cboGender.SelectedValue;

    hope this solves ur issue

    Narayanan Dayalan - Zeetaa Business Solutions Please Mark "Propose As Answer", if my answer works well with ur Query
    • Proposed as answer by VinilV Friday, November 13, 2009 10:39 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:28 AM
    Friday, November 13, 2009 10:34 AM
  • Hi,
    Narayanan is correct only .when you binding the value to combo box you need to define the value member as id.
    then only selectedvalue will work.
                 DataSet dsValue = new DataSet();
                dsValue = CreateDataSet();
                comboBox1.DataSource = dsValue.Tables["childTable"];
                comboBox1.ValueMember = "CustID";
                comboBox1.DisplayMember = "CustName";

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Proposed as answer by Harry Zhu Thursday, November 19, 2009 5:50 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:26 AM
    Friday, November 13, 2009 11:35 AM
  • Hi,

    You can set the datasource of combobox to an arraylist and set the text of textbox:
    I assumed you have a combobox named comboBox1 and a textbox named textBox1 .


      private void Form1_Load(object sender, EventArgs e)
            {
                ArrayList src = new ArrayList();
                src.Add(new cmbSource("single", "1"));
                src.Add(new cmbSource("pack", "2"));
                src.Add(new cmbSource("one", "3"));
                src.Add(new cmbSource("crate", "4"));
                this.comboBox1.DataSource = src;
                this.comboBox1.ValueMember = "ID";
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

    }
            public class cmbSource
            {
                private string myID;
                private string myName;

                public cmbSource(string strName, string strID)
                {
                    this.myID = strID;
                    this.myName = strName;
                }

                public string ID
                {
                    get
                    {
                        return myID;
                    }
                }
                public string Name
                {
                    get
                    {
                        return myName;
                    }
                }
            }


    Harry
              


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Thursday, November 19, 2009 5:50 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:26 AM
    Wednesday, November 18, 2009 6:57 AM

All replies

  • ASP.NET Code
    -----------------

    cboGender.Items.Add(new ListItem("Male", "M"));
    cboGender.Items.Add(new ListItem("Female", "F"));

    On the selection index change event of combo box
    ----------------------------------------------------------

    Textbox1.Text = cboGender.SelectedValue;

    hope this solves ur issue

    Narayanan Dayalan - Zeetaa Business Solutions Please Mark "Propose As Answer", if my answer works well with ur Query
    • Proposed as answer by VinilV Friday, November 13, 2009 10:39 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:28 AM
    Friday, November 13, 2009 10:34 AM
  • not what i want... my combobox dropdown gets populated using the ide and contains the values:

    single    (with id 1)
    pack     (with id 2)
    one       (with id 3)
    crate       (with id 4)

    it is from the items table

    now when i select single, i want the corresponding ID for that item to be displayed in the textbox ie 1....

    thanks
    Friday, November 13, 2009 10:51 AM
  • Hi,
    Narayanan is correct only .when you binding the value to combo box you need to define the value member as id.
    then only selectedvalue will work.
                 DataSet dsValue = new DataSet();
                dsValue = CreateDataSet();
                comboBox1.DataSource = dsValue.Tables["childTable"];
                comboBox1.ValueMember = "CustID";
                comboBox1.DisplayMember = "CustName";

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Proposed as answer by Harry Zhu Thursday, November 19, 2009 5:50 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:26 AM
    Friday, November 13, 2009 11:35 AM
  • Hi,

    Where are you stuck?
    Post your Code.

    Regards,
    Vinil;
    Friday, November 13, 2009 11:40 AM
  • Hi,

    You can set the datasource of combobox to an arraylist and set the text of textbox:
    I assumed you have a combobox named comboBox1 and a textbox named textBox1 .


      private void Form1_Load(object sender, EventArgs e)
            {
                ArrayList src = new ArrayList();
                src.Add(new cmbSource("single", "1"));
                src.Add(new cmbSource("pack", "2"));
                src.Add(new cmbSource("one", "3"));
                src.Add(new cmbSource("crate", "4"));
                this.comboBox1.DataSource = src;
                this.comboBox1.ValueMember = "ID";
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

    }
            public class cmbSource
            {
                private string myID;
                private string myName;

                public cmbSource(string strName, string strID)
                {
                    this.myID = strID;
                    this.myName = strName;
                }

                public string ID
                {
                    get
                    {
                        return myID;
                    }
                }
                public string Name
                {
                    get
                    {
                        return myName;
                    }
                }
            }


    Harry
              


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Thursday, November 19, 2009 5:50 AM
    • Marked as answer by Harry Zhu Friday, November 20, 2009 2:26 AM
    Wednesday, November 18, 2009 6:57 AM