[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

How to redisplay items in combo box when adding a new record

Hi Experts,

I have a program that displays a database in a detail view, and there is a few combo boxes on my main form.
The combo boxes have data bound items, and they work fine when the program runs, but my problem is that when i want to create a new record, the combo boxes go blank and there is now items left in the list...
How do i "refill" the combo boxes that the user can select a item from the drop-down list?

Thanx in advance
0
Benvor
Asked:
Benvor
  • 5
  • 3
1 Solution
 
Anurag ThakurCommented:
how are you creating a new record because of which the comboboxes go blank
0
 
angus_young_acdcCommented:
Simply re-call the code used to populate the combo box(es) in the first place.  If this is in your form load event, remove it and place it into a new method of its own.  Then you can call it as required.
0
 
BenvorAuthor Commented:
I use the binding navigator control at the bottom of the screen... (See attached image) and i don't use code to populate the combo boxes, i only select the correct options in the Properties dialog
untitled122.bmp
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
angus_young_acdcCommented:
In that case have you tried a .Refresh after you add a new item?  
0
 
BenvorAuthor Commented:
Nothing happens when I refresh..
0
 
angus_young_acdcCommented:
You could try the following:
bindingNavigator1.Invalidate();
bindingNavigator1.Update();

This is one of the reasons why I prefer to extract data from my DB via code.  Makes it easier should I ever have to do a refresh on what is contained in my controls.
0
 
angus_young_acdcCommented:
Actually scratch that, I just noticed you have an additional button to save your data.  That means pulling the data back won't display your newly created item until you save.  

Try something like a foreach loop where you add each item to a temporary List<>, then add your new item to this list (you can sort it if you wish).  Then as you are holding this data in another source you can foreach it again to populate your comboboxes.  After you save you can null this list as you should be only using data pulled back from the DB.
0
 
BenvorAuthor Commented:
Can u please show me the code to extract the data via code?
0
 
angus_young_acdcCommented:
Would be code similar to this.  However this is my own for my own needs and populates a datagrid, shouldn't be difficult to modify to populate the comboboxes
sql = "SELECT * FROM MyTable";
// then call the following
 
public void QueryDB(string sql)
        {
            OleDbConnection conn = null;
            try
            {
              
                if (_system == Program.TESTSYSTEM)
                {
                    conn = new OleDbConnection(Program.CONNECTIONSTRING +
                                                Program.TestSystemPath+
                                                "Config.mdb");
                }
                else
                {
                    conn = new OleDbConnection(Program.CONNECTIONSTRING +
                                                Program.SystemPath+
                                                "Config.mdb");
                }
                conn.Open();
                OleDbCommand cmd =
                    new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now