CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question Clearing a Combobox with a DataSource [C#]

    Given a Combobox [cbClient] that has been populated by a DataSet [ds] as such:
    Code:
    cbClient.DataSource = ds.Tables[0];
    cbClient.DisplayMember = "CLIENTS";
    cbClient.ValueMember = "CLIENTS";
    cbClient.SelectedIndex = -1;
    Problem is I can't seem to find a way to CLEAR/EMPTY the combobox when I want to, meaning if the user presses the "CLEAR" button I want the combobox [cbClient] to become empty.
    I tried stuff like "cbClient.Items.Clear()" but that gave errors like "Cannot modify the items collection when the DataSource property is set", I then tried using stuff like "cbClient.DataSource = null" but that does something really odd to the GUI of the Combobox (looks like the app crashed) and stuff like .Dispose() doesn't seem to work.
    Some have hinted that I need to edit the DATASOURCE (dataset ds) itself, I don't want to do it (seeing as I reuse it) I just want to clear the combobox so the user doesn't see anything in it at thay moment, maybe in the future I will rebind it
    Any hints/Help/Clues would be much appreciated. Thanks,

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

  3. #3
    Join Date
    Oct 2004
    Posts
    429

    Re: Clearing a Combobox with a DataSource [C#]

    And it didn't do anything odd to your Combobox?
    Mine is "frozen", the picture on my form isn't even there properly..
    ....odd....

  4. #4
    Join Date
    Sep 2013
    Posts
    1

    Re: Clearing a Combobox with a DataSource [C#]

    comboBox1.DataSource = null; will solve ur problem..if you not yet find solution , plz find the following c# combobox fill from datasource.

    Manly

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Clearing a Combobox with a DataSource [C#]

    Thanks for your eagerness to help manlypullock. Remember though, that this thread is 8 years old and the problem has long since been resolved. Please do not revive old threads - there are plenty of current threads that need help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured