views:

34

answers:

1

Hi, 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!

+2  A: 

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.
ho1
Thanks for the quick response. I am binding as in 1. I would like to avoid adding the empty row to the database and was hoping that I could do it with the code.
Juan Besa
@Juan: You misunderstood what I meant. When I said `DataTable` I meant an instance of the DataTable class (as shown here http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx). That way you can add the record just to the .Net instance of the table and since you're not writing that table back to the DB, it won't affect your DB in any way.
ho1
See the top answer in this discussion list for a sample showing what I mean: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/4d4462ba-f4d7-4f1d-8747-8fe63272a7f4
ho1