2013-01-03 25 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作为属性访问了文本框和标签。现在正在工作 –

相关问题