Tutorial :Add nothing selected as default in a .net comboBox



Question:

I am working in C# with Visual Studio 2008.

I am trying to create a comboBox for a simple search form in a windows application. What I want to do is have a comboBox with some options from a table in my database so I bound a table to it and selected the columns to display. What I want to do is add a default blank value(to represent any of the options) when the form is first loaded. That way the comboBox would act as a way to refine the search a bit.

Is there any way to do this? I have tried several way (including adding the code by hand) but it throws exceptions because of the data binding.

Is there any property that might allow me to do this?

Thanks!


Solution:1

I'm not sure if I understand completely, so a few possibilities.

Would setting SelectedIndex to -1 solve your problem?

Otherwise, how do you bind it to the DB? Example solutions depending on how you bind:

  1. If you're retrieving a DataTable and binding to that table, just add a empty row at the top of the table.
  2. If you're not able to do 1, then if you're using a SQL statement to retrieve the data, maybe you can just add a bit to the SQL statement to return an empty row.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »