1

In my parent form I have comboBox populated with items using DataTable as DataSource which looks like this.

        sourceTypes = myDataBase.SourceType.ToList<SourceTypes>();

        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("ID");

        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);

        foreach (SourceType sourceType in sourceTypes)
        {
            string sourceTypeName = sourceType.sourceTypeName;
            string ID = sourceType.sourceTypeID.ToString();
            dt.Rows.Add(sourceTypeName, ID);
        }

        comboBox3.DataSource = dt;
        comboBox3.ValueMember = "ID";
        comboBox3.DisplayMember = "Name";

From my parent form I am calling my child form in which I add new record to myDataBase and I would want this new added record to be visible in my parent's form comboBox as soon as child form is closed. So I was thinking to call something like this from my childFrm which will refresh the comboBox.

        private void FrmChild_FormClosed(object sender, FormClosedEventArgs e)
        {
             FrmParent.refresh();
        }

Now I need the static refresh method in my parent form, which I can't really get to work. How could I get it done? Cheers

EDIT: Child form is opened here:

    private void button7_Click(object sender, EventArgs e)
    {
        FrmChild frmChild = new FrmChild();
        frmChild.ShowDialog();
        frmChild.Dispose();
    }

EDIT2: I tried like this (no static method), but didn't work

In my Child form:

        private void FrmChild_FormClosed(object sender, FormClosedEventArgs e)
        {
             FrmParent frmParent = new FrmParent();
             frmParent.refresh();
        }

In my parent form, refresh method (after the new record is added to myDataBase).

        sourceTypes = myDataBase.SourceType.ToList<SourceTypes>();

        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("Name");
        DataColumn dc2 = new DataColumn("ID");

        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);

        foreach (SourceType sourceType in sourceTypes)
        {
            string sourceTypeName = sourceType.sourceTypeName;
            string ID = sourceType.sourceTypeID.ToString();
            dt.Rows.Add(sourceTypeName, ID);
        }

        comboBox3.DataSource = dt;
        comboBox3.ValueMember = "ID";
        comboBox3.DisplayMember = "Name";

Tried like this, combobox still doesnt get refreshed.

4

1 回答 1

0

您可以将父表单传递给子表单并调用其方法。在您的子表单中创建一个构造函数,该构造函数接受您的父表单并且不要使您的refresh方法静态。也尝试处理Closing事件而不是Closed事件。

FrmChild 类:

private FrmParent parentForm = null;
public FrmChild(FrmParent pf)
{
   parentForm = pf;
}

现在,when closing,您可以调用 refresh 方法

private void FrmChild_FormClosing(object sender, FormClosingEventArgs e)
{
   if(parentForm != null)
      parentForm.refresh();
}

FrmParent 类

private void button7_Click(object sender, EventArgs e)
{
    using(FrmChild frmChild = new FrmChild(this))
    {
       frmChild.ShowDialog();     
    }
}
于 2013-04-21T23:09:02.983 回答