简体   繁体   中英

The Data Bound ComboBox doesn't update

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM