如何在Windows Forms c#中的DataRepeater内绑定userControl? [英] How to bind userControls inside a DataRepeater in Windows Forms c#?
问题描述
如何在Windows Forms c#中的DataRepeater内绑定userControls?
How to bind userControls inside a DataRepeater in Windows Forms c#?
我不想使用这种方法: http://blogs.msdn.com/b/vsdata/archive /2009/08/12/datarepeater-control-for-windows-forms.aspx ,但我想以编程方式从DataSet / DataTable进行绑定。
在中继器里面只是为了测试我放一个文本框和一个标签。还有一个UserControl也由两个控件组成:一个标签和一个文本框。
到目前为止,只有第一个标签和文本框已更新,但不是用户控件。我想念什么?
I don't want to use this method: http://blogs.msdn.com/b/vsdata/archive/2009/08/12/datarepeater-control-for-windows-forms.aspx but I want to make the binding from a DataSet/DataTable programmatically. Inside the repeater just for tests I put one textbox and one label. Also one UserControl which consists also of two controls: one label and one textbox. So far only first label and textbox are updated but not the usercontrol. What do I miss?
这是加载事件
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,...?
。。
Should I also use other events like DrawItem, ... ? regards.
推荐答案
使用 BindingSource 添加绑定:
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = items;
textBox1.DataBindings.Add("Text", bindingSource1, "FirstName");
label1.DataBindings.Add("Text", bindingSource1, "LastName");
dataRepeater1.DataSource = bindingSource1;
这篇关于如何在Windows Forms c#中的DataRepeater内绑定userControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!