Pregunta

How to bind userControls inside a DataRepeater in Windows Forms c#?

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?

This is the Load event

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;
        }

Should I also use other events like DrawItem, ... ? regards.

¿Fue útil?

Solución

Use BindingSource to add bindings:

BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = items;
textBox1.DataBindings.Add("Text", bindingSource1, "FirstName");
label1.DataBindings.Add("Text", bindingSource1, "LastName");
dataRepeater1.DataSource = bindingSource1;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top