2017-04-06 45 views
0

我已經使用了用戶控件從這個鏈接https://www.codeproject.com/articles/739902/how-to-easily-host-wpf-control-inside-windows-form 我只能在構造函數此用戶控件加載數據,但我想從其他方法中調用它,但我失敗了有什麼辦法? 的construcor是如何將數據綁定到無構造用戶控件?

public Getstarted() 
{ 
    InitializeComponent(); 
    List<Employee2> employees = new List<Employee2>(); 
    try 
    { 
     con = new SqlConnection(cs.DBConn); 
     con.Open(); 
     cmd = new SqlCommand("SELECT Participant.ParticipantName, MeetingParticipant.Title, Participant.ParticipantId FROM   MeetingParticipant INNER JOIN Participant ON MeetingParticipant.ParticipantId = Participant.ParticipantId INNER JOIN Meeting ON MeetingParticipant.MeetingId = Meeting.MeetingId where Meeting.Statuss='Open'", con); 
     rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

      while (rdr.Read() == true) 
      { 
       employees.Add(new Employee2() { Name = rdr[0].ToString(), Title = rdr[1].ToString(), Id = rdr[2].ToString()}); 
      } 
      con.Close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    this.comboBoxWithGrid_WinformsHost1.Employee2s = employees; 
} 

我已經試過這對按鈕事件,但未能

private void MyMethod() 
{  
    List<Employee2> employees = new List<Employee2>(); 
    try 
    { 
     con = new SqlConnection(cs.DBConn); 
     con.Open(); 
     cmd = new SqlCommand(" SELECT Participant.ParticipantName, MeetingParticipant.Title, 
     Participant.ParticipantId FROM   MeetingParticipant 
      INNER JOIN 
      Participant ON MeetingParticipant.ParticipantId = 
      Participant.ParticipantId INNER JOIN Meeting ON 
      MeetingParticipant.MeetingId = Meeting.MeetingId where 
      Meeting.Statuss='Open'", con); 
     rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

     while (rdr.Read() == true) 
     { 
      employees.Add(new Employee2() { Name = rdr[0].ToString(), Title = rdr[1].ToString(), Id = rdr[2].ToString()});  
     } 
     con.Close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
     this.comboBoxWithGrid_WinformsHost1.Employee2s = employees; 
} 

這是我的第一個問題在這裏,我是新手在這個領域如果有缺少的東西。請理解我的限制謝謝您。

+0

人們普遍認爲標準提供任何錯誤消息或錯誤而不是說的說明解決了我的問題,通過 「失敗」 。 –

+0

我沒有任何錯誤消息。 –

+0

那麼請記住,包括之間將會發生什麼,什麼沒發生的差異。 –

回答

0

這不是直接回答我的問題,但我用這條線

 this.comboBoxWithGrid_WinformsHost1.Employee2s.AddRange(employees); 
相關問題