[.net] Databinding ComboBoxes

Started by
5 comments, last by static_matt13 18 years, 8 months ago
I am having a problem with a database program I'm writing. I am using a MDI interface to manipulate the database. I use comboboxes to select relational data for data-entry. I fill these comboboxes by databinding to tables in my database. I need to use databinding because it's very possible that items could be added to the tables and my comboboxes need to be up-to-date. The real problem comes with setting the comboboxes to index -1. I need to use this because another one of my uses for comboboxes is selecting data to filter by in a datagrid. I need the comboboxes to default to null data (no filter). I can manually set the databound combobox index to -1, but this is where there is some strange behavior. I set the index to -1, if the combobox index was 0, then I get the expected behavior of combobox showing nothing. However, if the index isn't 0, then the combobox will display the item at index 0. The index is actually -1, but the display value is still 0. I have a hack solution to this, any time I want to reset a combobox index to -1, I set the selectedindex to -1 TWICE. This functionally works quite well, but it's quite hackish. First Question: Does anyone know of a more elegant solution to this? Now here is my real problem. There is another behavior with modifying a table the combobox is bound to. If the data is changed within a table, the combobox bound to this table will reset to index 0 if it is set to index -1. This is a huge problem with my datagrid filters since they set the filter on IndexChanged event. Second Question: Is there a way to keep a combobox at index -1 the databound table is changed, but still let the index be selected by the user and programmably? EDIT: btw, I'm using VS 2003 and C# [Edited by - static_matt13 on July 25, 2005 2:54:12 PM]
"God''s in his Heaven. All''s right with the World"
Advertisement
Please help with this. I have thought up a potential solution. If I could inherit ComboBox class into a custom control and overload the databinding interface. If I could do that, I could add a null entry into index 0 without having to modify my DataSet data. Problem is, I have no idea how to go about this. Any help??
"God''s in his Heaven. All''s right with the World"
Have you tried setting ComboBox.SelectedValue to DBNull instead of setting the index to -1?
Why don't you just insert an item at position 0 in the combobox that says "Select Below" or something with a value of null?
>>Have you tried setting ComboBox.SelectedValue to DBNull instead of setting the index to -1?

Yeah, I have tried that before. It has the exact same behavior.

>>Why don't you just insert an item at position 0 in the combobox that says "Select Below" or something with a value of null?

Because I am databinding. You can't add anything into a databound combobox.
"God''s in his Heaven. All''s right with the World"
Haven't tried it in ASP.NET 1.1, but you can choose to have databound items appended to a drop down list in ASP.NET 2.0.
I'm not using ASP.NET, I'm doing a WinForms application.

Whatever, I finally fixed the problem and I'm posting it here for other people. I created a custom control by inheritiing the Combobox and then overloading all the databinding attributes. I think I'm gonna post my final version of the control on codeproject.com since this seems to be a common gripe. I'll post a link when and if I upload it.
"God''s in his Heaven. All''s right with the World"

This topic is closed to new replies.

Advertisement