none
How to retrieve value from ComboBox in C#

    Question

  • Hi,
    I have a problem to get value in C# but not in VB.net.

    Case: I have loaded the data into ComboBox from SqlCe Data

    Example :
    cboCountry.DataSource = DS.Tables["tblCountry"];
    cboCountry.DisplayMember = "Country";
    cboCountry.ValueMember = "Cid";

    To retrieve the value of the selected item from the combo box

    In VB.net
    ...............

      Dim Idno As Integer = CboCountry.SelectedValue

    This will give me the Value of the Id  which is the value member in CboCountry


    in C#, I can not get any thiing??

    string StrId = CboCountry.SelectedValue(); <--- this wont work!!!

    How do I get the value and How can I covert this StrId into integer?


    thanks






    Peace is the fruit of Love
    Wednesday, September 16, 2009 5:37 AM

Answers

  • SelectedValue normally works, but doesn't always give you the type it should. In the code below, even though I entered Country.Id as an integer, SelectedValue returns a string.

    Example:

    DataTable
    dataTable = new DataTable("Country");
    dataTable.Columns.Add(
    "Id");
    dataTable.Columns.Add(
    "Name");
    dataTable.Rows.Add(45,
    "Denmark");
    dataTable.Rows.Add(63,
    "Philippines");

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

    comboBox1.SelectedIndex = 1;
    comboBox1.Refresh();

    DataRow
    selectedDataRow = ((DataRowView)comboBox1.SelectedItem).Row;
    int countryId = Convert.ToInt32(selectedDataRow["Id"]);
    string countryName = selectedDataRow["Name"].ToString();

    int
    selectedValue = Convert.ToInt32(comboBox1.SelectedValue);

    Wednesday, September 16, 2009 9:57 AM

All replies

  • Hi,

    I would probably use a line as follows :-

    this.label1.Text = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
    This is demonstrated in the project ComboBox Selected Item Tester here :-

    http://www.smartmobiledevice.co.uk/Projects

    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Wednesday, September 16, 2009 6:05 AM
  • Hi Paul,

    It seems it wont work.

    This is my form outlay :   

    1)    CboCountry

    2)    BtnSelect.

    In BtnSelect , this is the code:

    private void BtnSelect_Click(object sender, EventArgs e)

            {
                int cboIndex = cboCountry.SelectedIndex;          
                string strId = this.cboCountry.Items[cboIndex].ToString();

                MessageBox.Show("Select Id :" + strId);

                //ShowCity(StrId);
                
            }


    I got error msg : "system.Data.DataRowView


    My Objective:  If the user select a country in the cboCountry, I need to get the Id of the country so that I can pass it to the ShowCity(StrId)

    How Do I get the Id ? Do I need to convert the result return from the cbo.items[] method??


    Thanks






    Peace is the fruit of Love
    Wednesday, September 16, 2009 6:55 AM
  • How did you populate your ComboBox?

    Normally, I know how exactly what the ComboBox item collection contains and I use ComboBox.SelectedItem to retrieve it and cast it into the type I need.
    Wednesday, September 16, 2009 9:00 AM
  • Hi Christian Resma Helle,

    This is the way I populate the combobox:

                   SqlCeConnection cn;

                    cn = new SqlCeConnection("Data Source=" + StrDbPath + "\\Cities.sdf;");

                    cn.Open();

                    SqlCeCommand cmd = null;
                   cmd = cn.CreateCommand();              
                    cmd.CommandText = "Select Cid,Country from tblCountry";

                    SqlCeDataAdapter MyAdapter = new SqlCeDataAdapter(cmd);
                    DataSet DS = new DataSet();
                    MyAdapter.Fill(DS, "tblCountry");
                    cn.Close();
                  
                    cboCountry.DataSource = DS.Tables["tblCountry"];
                    cboCountry.DisplayMember = "Country";
                    cboCountry.ValueMember = "Cid";

    How do i get the Id  when user click and select in combobox.

    Thank



    Peace is the fruit of Love
    Wednesday, September 16, 2009 9:23 AM
  • DataRowView has a property called Row which returns the DataRow, from there you just retrieve the value from the column.

    Example:

    DataTable dataTable = new DataTable("Country");
    dataTable.Columns.Add("Id");
    dataTable.Columns.Add("Name");
    dataTable.Rows.Add(45, "Denmark");
    dataTable.Rows.Add(63, "Philippines");  
    comboBox1.DataSource = dataTable;
    comboBox1.DisplayMember = "Name";

    comboBox1.SelectedIndex = 1;
    comboBox1.Refresh();  

    DataRow selectedDataRow = ((DataRowView)comboBox1.SelectedItem).Row;
    int countryId = Convert.ToInt32(selectedDataRow["Id"]);
    string countryName = selectedDataRow["Name"].ToString();

    Wednesday, September 16, 2009 9:44 AM
  • SelectedValue normally works, but doesn't always give you the type it should. In the code below, even though I entered Country.Id as an integer, SelectedValue returns a string.

    Example:

    DataTable
    dataTable = new DataTable("Country");
    dataTable.Columns.Add(
    "Id");
    dataTable.Columns.Add(
    "Name");
    dataTable.Rows.Add(45,
    "Denmark");
    dataTable.Rows.Add(63,
    "Philippines");

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

    comboBox1.SelectedIndex = 1;
    comboBox1.Refresh();

    DataRow
    selectedDataRow = ((DataRowView)comboBox1.SelectedItem).Row;
    int countryId = Convert.ToInt32(selectedDataRow["Id"]);
    string countryName = selectedDataRow["Name"].ToString();

    int
    selectedValue = Convert.ToInt32(comboBox1.SelectedValue);

    Wednesday, September 16, 2009 9:57 AM
  • hi Christian Resma Helle

    Thank you very much. This problem is solved. This method provide great interactivity.
    Peace is the fruit of Love
    Thursday, September 17, 2009 2:24 AM
  • thanks a lot

    but would you describe why combobox.selectedvalue not working?

    Tuesday, September 24, 2013 5:42 AM