Jump to content
  • Advertisement
Sign in to follow this  
static_matt13

[.net] Databinding ComboBoxes

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Have you tried setting ComboBox.SelectedValue to DBNull instead of setting the index to -1?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
>>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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!