Custom control generated from datatable
I need a help with functionality where i have data in dataset table. For each row i need to create control with some data and all of this controls must be in flow layout. I want to make custom control from this, that can bind to datasource like dataset.
More info: I have made my custom control
[开发者_JAVA技巧System.ComponentModel.ComplexBindingProperties("DataSource")]
public partial class UserControl1 : System.Windows.Forms.FlowLayoutPanel
{
public object DataSource
{
get { return datatable; }
set
{ datatable = (List<String>)value;
MakeControls();
}
}
private void MakeControls()
{
if (datatable == null)
return;
this.SuspendLayout();
this.Controls.Clear();
foreach(String str in datatable)
{
GroupBox gb = new GroupBox();
gb.Text = str;
this.Controls.Add(gb);
}
this.ResumeLayout();
}
}
I dont know is this enough. So Datasource is only property with method that is fired when its set.
You can loop through the rows in the dataset table, constructing a new control for each one, and add it to a FlowLayoutPanel on your form.
foreach (DataRow dr in ds.Tables[0].Rows)
{
Textbox t = new TextBox(); //Or whatever control you want
t.Text = dr.Value; // NB: Not actual code, I'm not at my IDE
flowPanel.Controls.Add(t);
}
精彩评论