winformsc#-4.0localreport

Updating controls in child form from parent form


I have below code in 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();
        }
    }

Then in Form1 which is a parent of Form2, i have below code in radiobutton

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

The problem is that when i check a radioButton control (radioButton1) in Form1, authorisedList() in Form2 seems to be executing but the reportViewer report does not update/change.

Am wondering why.


Solution

  • If your Form2 is already open , then you should get the object of the open form and then call its authorisedList() method. You can use Application.OpenForms property.

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