简体   繁体   中英

update combobox from another form in c#

I want to update combobox of the main form from another form this is my main form combobox update code which is in the form_load

public void main_Load(object sender, EventArgs e)
{
    try
    {
        OleDbDataAdapter db = new OleDbDataAdapter("select * from category", con);
        ds_cmbGroup.Clear();
        cmbGroup.DataSource = null;
        db.Fill(ds_cmbGroup, "t1");
        cmbGroup.DisplayMember = "catname";
        cmbGroup.ValueMember = "catcode";
        cmbGroup.DataSource = ds_cmbGroup.Tables["t1"];
    }
    catch (Exception ex)
    {
    }
}

as I call this method in its own form it runs correctly. this is the code to call it:

main_Load(this, null);

but when I call it from another form, (though the dataset updates with no problem) it doesn't change the combobox(cmbGroup) content this is the code to load main_Load method from second form:

main Main = new main();
Main.main_Load(this, null);

Thanks in Advance

The problem is that you dont reference the current instance of main form but you create a new instace, on second form try:

 Main  obj = (Main)Application.OpenForms["Main"];

and then use obj to refer to main Form

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