none
Set Combobox SelectedValue to null

    Question

  • Hi everyone...

    Basically I've got a small problem with the combobox. When the window is loaded this combobox is populated from a database... There is a radiobutton righ underneath the combobox and if the radiobutton is checked the combobox shouldn't have any selected value....

    I've tried a few things like:

     combobox.SelectedIndex = -1;

    combobox.SelectedValue = 0; and a few others...

    But none of them seems to work... Does anybody know how to do this without clearing the combobox and repopulating it again?

     


    Rising Storm Technologies
    Friday, March 25, 2011 1:10 PM

Answers

  • The code "combobox.SelectedIndex = -1;", should do the job. If you are setting the "combobox.SelectedValue = 0", then that could be the cause.


    Ajosh Jose
    • Marked as answer by linkit Monday, March 28, 2011 6:52 AM
    Friday, March 25, 2011 1:28 PM
  • I can confirm that Jose's answer works. Here is the code I used in context:

    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {
      if (checkBox1.IsChecked == true)
      {
        comboBox1.SelectedIndex = -1;
      }
    }
    
    This successfully clears a selected item out of the combo box while retaining the Items collection.

    • Marked as answer by linkit Monday, March 28, 2011 6:52 AM
    Friday, March 25, 2011 2:46 PM

All replies

  • The code "combobox.SelectedIndex = -1;", should do the job. If you are setting the "combobox.SelectedValue = 0", then that could be the cause.


    Ajosh Jose
    • Marked as answer by linkit Monday, March 28, 2011 6:52 AM
    Friday, March 25, 2011 1:28 PM
  • I can confirm that Jose's answer works. Here is the code I used in context:

    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {
      if (checkBox1.IsChecked == true)
      {
        comboBox1.SelectedIndex = -1;
      }
    }
    
    This successfully clears a selected item out of the combo box while retaining the Items collection.

    • Marked as answer by linkit Monday, March 28, 2011 6:52 AM
    Friday, March 25, 2011 2:46 PM
  • might you have done something wrrong on combobox selectedIndex.

    If your issue persists, could you share some codes with me, as others said, selectIndex could work...


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, March 27, 2011 3:19 AM
    Moderator
  • Hi everyone!

    Thanks so much for the help!!! nheembrock734 solution worked... The checkbox had to be looked if it was checked, thanks!!!

    Kind Regards


    Rising Storm Technologies
    Monday, March 28, 2011 6:53 AM