1

I have a DataTable with Item information, and a form to order Items. On the Order form there is a ComboBox displaying the Item Names:

 void fillComboItem()
        {
            string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
            string Query = "select * from stockTBL; ";
            SqlCeConnection conDataBase = new SqlCeConnection(constring);
            SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase);
            SqlCeDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {
                    string sName = myReader.GetString(myReader.GetOrdinal("Item Name"));
                    comboItem.Items.Add(sName);
                }

                //displays a system error message if a problem is found
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

When I insert data into the DataTable, it does not update the ComboBox in the Order form and I have to restart the application for it to update. How can I "refresh" the ComboBox?

Tom
  • 154
  • 2
  • 7
  • 25
  • 1
    What data table? You haven't shown one in your code. Also - you're manually adding the items from a _database table_.. adding data into that table won't re-run your code automatically. – Simon Whitehead Apr 10 '14 at 22:54
  • The "stock TBL", its in a local database "LWADataBase". How do I "re-run" or something to that effect? – Tom Apr 10 '14 at 22:57
  • 1
    How are you calling this code in the first place? You simply call `comboItem.Items.Clear();` then call `fillComboItem()` again. – Simon Whitehead Apr 10 '14 at 22:58
  • I must point out the DataGridView for the stockTBL is displayed on a different form. And I call it in on form_Load. – Tom Apr 10 '14 at 23:01
  • Works perfectly, could you post it as an answer so I can answer the question? – Tom Apr 10 '14 at 23:40

1 Answers1

0

As far as I understand, you're talking about updating your UI when DB changes. You cant do that by explicitly calling your databinding function - fillComboItem in your case - when you want combobox updated, say, when user opens your grid or clicks around. Alternatively, you can ping DB for updates, but this is not a lightweight approach and should be used carefully.

ATu
  • 1
  • I have a solution, just waiting for the time to elapse before answering my own question. – Tom Apr 10 '14 at 23:59