none
combo box select item

    Question

  • HI,
    In winform, a combo box is populated using a datatable.
    There is only one field which is of type string.

    How can I show a particular item in that combobox?
    For example, there are 10 names in the combo box.
    I want to show say the name "Roger".
    How is this done so that the name Roger is selected in the combobox?

    Thanks

    Monday, July 09, 2012 9:57 AM

Answers

All replies

  • Try

    combo1.Text = "Roger"

    If Roger is in the list, the combo box will select it.  I don't recall exactly what happens if Roger isn't in the list.  If that's a possibility in your case, you'll have to experiment and handle correctly.


    This signature unintentionally left blank.

    Monday, July 09, 2012 10:31 AM
  • Try this example

    comboBox2.DataSource = ds.Tables[0];//Your Data Source comboBox2.DisplayMember = "Names";//Column Name comboBox2.ValueMember = "EmpId";//Column Name comboBox2.SelectedValue = 3;//Value for your selected Column



    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Monday, July 09, 2012 10:32 AM
  • There is many ways to do that. You can use SelectedIndex property if you know the index of the "Roger" item, you can use the SelectedValue property to "Roger" because the values are strings or you can use the SelectedItem property, if you have the item to set as selected.

    You can see documentation for more and for examples http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

    • Marked as answer by arkiboys Monday, July 09, 2012 10:42 AM
    Monday, July 09, 2012 10:34 AM