简体   繁体   中英

c# Entity framework refresh combobox after adding data from another form

i'm using this code to load data from database to combobox

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 comboBox1.DataSource = Dbcontext.Products.Tolist();

 comboBox1.DisplayMember = "ProductName";
 comboBox1.ValueMember = "ID"; 

}

when i add a new product from another windowsForm in the same project , when i come back to select the value from the combo box I can't found it. and this refresh won't work

privatevoid ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {         ComboBox1.Refresh()     

   }

how can i do this? thank you

Setting the AutoPostback property to true will cause it to postback

you need to be sure to load the dropdownlist only if (!ispostback)

ListControl.AutoPostBack Property

您可以尝试使用ComboBox1.UpdateLayout()方法。

You need to repopulate the comboBox1, when you move from that screen to this screen.

Your combobox datasource doesnt contain the newly added item. "Dbcontext.Products.Tolist()" populates the present products but isnt binded to fetch the newly added ones.

You need to perform that action manually. Call a method on your current window like this

void RefreshData()
{
 comboBox1.DataSource = null;
 comboBox1.DataSource = Dbcontext.Products.Tolist();
}

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