2013-01-03 27 views
1

如何綁定Windows窗體c#中的DataRepeater內的userControls?如何綁定Windows窗體c#中的DataRepeater內的userControls?

我不想使用此方法:http://blogs.msdn.com/b/vsdata/archive/2009/08/12/datarepeater-control-for-windows-forms.aspx但我想以編程方式從DataSet/DataTable進行綁定。 爲了測試,我在裏面放置了一個文本框和一個標籤。另外一個UserControl也由兩個控件組成:一個標籤和一個文本框。 到目前爲止只有第一個標籤和文本框被更新,但不是用戶控件。我錯過了什麼?

這是Load事件

private void DataRepeaterForm_Load(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection("Data Source=ADRIAN-PC; Initial Catalog=AdventureWorks2008R2; Integrated Security=true;User id=;password="); 

      SqlCommand cmd = new SqlCommand("Select * from Person.Person", con); 
      SqlDataAdapter adapt = new SqlDataAdapter(cmd); 

      DataTable items = new DataTable(); 
      adapt.Fill(items); 

      textBox1.DataBindings.Add("Text", items, "FirstName"); 
      label1.DataBindings.Add("Text", items, "LastName"); 

      TextBox myUcTextBox1 = (TextBox)myUC1.Controls.Find("textBox1", true).First(); 

      if(myUcTextBox1 != null) 
       myUcTextBox1.DataBindings.Add("Text", items, "LastName"); 

      dataRepeater1.DataSource = items; 
     } 

我應該也可以使用其他的事件,如DRAWITEM,...? 關於。

回答

4

使用BindingSource添加綁定:

BindingSource bindingSource1 = new BindingSource(); 
bindingSource1.DataSource = items; 
textBox1.DataBindings.Add("Text", bindingSource1, "FirstName"); 
label1.DataBindings.Add("Text", bindingSource1, "LastName"); 
dataRepeater1.DataSource = bindingSource1; 
+0

這不救我的問題,裏面的DataRepeater用戶控件的控件沒有約束。 –

+0

@AlexaAdrian驗證您的數據表中是否有任何數據 –

+1

我這樣做。在這個意義上,我得到了一個解決方案:我在userControl中創建了屬性,因此我使用get和set作爲屬性訪問了文本框和標籤。現在正在工作 –

相關問題