darkdog
darkdog

Reputation: 4015

binding data to a usercontrol

i've a userControl (watch the screenshot): enter image description here

I would like to know how can i bind data to this control.

1) i want to drag & drop this usercontrol to a form 2) i wish to decide which table from my database should be used 3) the datagrid (the grey control at bottom) should be automatically binded to the datasource which points to the table i wish to use 4) the detail Tab at the usercontrol should be automatically filled with label + textbox for each field of the table i wish to use

My Question is.. how can i realize this? Any ideas?

greets

Upvotes: 0

Views: 1756

Answers (1)

Arie
Arie

Reputation: 5373

1) When you build your project, your UserControl is displayed in the VisualStudio's Toolbox, so you can drag&drop it on your forms wherever you want it.

2) Expose DataSource of your DataGridView, for example in some property of your UserControl:

public BindingSource MyDataGridViewDataSource
{
   get 
   {
      return MyDataGridView.DataSource; // or you can skip 'get' if you don't need it
   }
   set
   {
      MyDataGridView.DataSource = value;
   }
}

3) ...or if you want column names, use property that gets/sets DataTable (remember to Fill it first http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx):

private DataTable _myDataTable = new DataTable();
public DataTable MyDataTable
{
  get
  {
   return _myDataTable;
  }
  set
  {
   _myDataTable = value;
   BindingSource bs = new BindingSource();
   bindingSource1.DataSource = value;
   MyDataGridView.DataSource = bs;

   // 4) fill your labels somewhere here
   string tablename = value.TableName; 
   foreach (DataColumn col in value.Columns)
     Console.WriteLine("{0}\t{1}", col.ColumnName, col.DataType);
  }
}

It is not a complete solution, but it's a place to start.

Upvotes: 3

Related Questions