Schoof
Schoof

Reputation: 2855

Usercontrol Datasource?

In my current windows application I am looking on how to give a datasource to my usercontrol. On my page I add my own usercontrol in a flowlayoutpanel, the usercontrol has 3 textboxes in it which I want to fill with data from a datasource.

 usercontrol uc = new usercontrol();
 flowlayoutpanel.Controls.Add(uc);
 uc.DataSource?

I know that in silverlight and ASP.NET you can add a datasource to a usercontrol. In the usercontrol you get the data into the textboxes by using {Binding fieldname} as their content. I can't find any information on this for Windows Forms.

Thanks for the help. Thomas

Upvotes: 1

Views: 3760

Answers (2)

Schoof
Schoof

Reputation: 2855

I solved this (thanks to the link of Stuart) by putting setters and getters in my usercontrol.

public partial class ucOpleiding : UserControl
{
   public string Datum
   {
        get { return txtDatum.Text; }
        set { txtDatum.Text = value; }
   }

   public string Plaats
   {
       get { return txtPlaats.Text; }
       set { txtPlaats.Text = value; }
   }

   public string Omschrijving
   {
       get { return txtOmschrijving.Text; }
       set { txtOmschrijving.Text = value; }
   }

    public ucOpleiding()
    {
        InitializeComponent();
    }

And in my main form I will then call those setters and getters.

foreach (opleiding opl in ChauffeurManagement.getOpleidingen(Int32.Parse(cbbID.SelectedValue.ToString())))
{
     ucOpleiding uc = new ucOpleiding();

     uc.Datum = opl.datum.ToString();
     uc.Plaats = opl.plaats_instantie;
     uc.Omschrijving = opl.omschrijving;

     flpOpleidingen.Controls.Add(uc);
}

Upvotes: 0

stuartd
stuartd

Reputation: 73253

There's an article on MSDN which might help you implement this - Walkthrough: Creating a User Control that Supports Simple Data Binding (see also Complex and Lookup Data binding walkthroughs).

Upvotes: 3

Related Questions