从父窗体更新子窗体中的控件

时间:2013-02-20 10:08:44

标签: winforms c#-4.0 localreport

我在Form2中有以下代码

public void authorisedList()
    {
        using (myContext v = new myContext())
        {
            DateTime date = DateTime.Today.AddMonths(-12);

            var myList = (from l in v.AuthorisedList
                                            where l.FromDate >= date
                                            select new
                                            {
                                                l.ID,
                                                l.EmpName,
                                                l.StartDate,
                                                l.EndDate,
                                                l.Days,
                                                l.Approved,
                                                l.Confirmed,
                                            }).ToList();

            reportViewer1.LocalReport.DataSources.Clear();
            ReportDataSource datasource = new ReportDataSource("MyReportsDatasource", myList);
            reportViewer1.LocalReport.DataSources.Add(datasource);

            string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
            string reportPath = Path.Combine(exeFolder, @"rdlcReports\Authorised List.rdlc");

            reportViewer1.LocalReport.ReportPath = reportPath;
            reportViewer1.RefreshReport();
        }
    }

然后在Form1中,它是Form2的父级,我在radiobutton中有以下代码

 private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        Form2 au = new Form2(this);
        au.authorisedList();
    }

问题是当我在Form1中检查radioButton控件(radioButton1)时,Form2中的authorisedList()似乎正在执行,但reportViewer报告不会更新/更改。

我想知道为什么。

1 个答案:

答案 0 :(得分:0)

如果您的Form2已经打开,那么您应该获取打开表单的对象,然后调用其authorisedList()方法。您可以使用Application.OpenForms属性。

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    Form2 au = Application.OpenForms["Form2"] as Form2;
    if(au != null)
           au.authorisedList();
}