Skip to main content
 首页 » 编程设计

c#之如何在 Windows 窗体 c# 中的 DataRepeater 中绑定(bind) userControls

2024年02月20日17arxive

如何在 Windows 窗体 c# 中的 DataRepeater 中绑定(bind)用户控件?

我不想使用这个方法:http://blogs.msdn.com/b/vsdata/archive/2009/08/12/datarepeater-control-for-windows-forms.aspx但我想以编程方式从 DataSet/DataTable 进行绑定(bind)。 在中继器内部仅用于测试,我放了一个文本框和一个标签。还有一个 UserControl,它也由两个控件组成:一个标签和一个文本框。 到目前为止,只更新了第一个标签和文本框,但没有更新用户控件。我想念什么?

这是加载事件

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,...? 问候。

请您参考如下方法:

使用BindingSource添加绑定(bind):

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