2013-02-20 96 views
1

我有以下代碼在窗體2更新控制

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中這是窗體2的父,我有以下代碼中的單選按鈕

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

的問題是當我檢查Form1中的radioButton控件(radioButton1)時,Form2中的authorisedList()似乎正在執行,但reportViewer報告不更新/更改。我不知道爲什麼。

回答

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(); 
}