2011-04-09 28 views
0

我需要幫助功能,其中我有數據集中的數據表。 對於每一行我都需要用一些數據創建控件,並且所有這些控件都必須在流佈局中。我想從這個自定義控件,它可以綁定到數據源像數據集。從數據表生成的自定義控件

更多信息: 我已經做了我的自定義控件

[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(); 
    } 
     } 

我不知道這是不夠的。所以Datasource只是屬性,當它被設置的時候會被觸發。

回答

1

您可以遍歷數據集表中的行,爲每個表構造一個新的控件,並將其添加到表單上的FlowLayoutPanel。

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