Search code examples
c#dynamictablelayoutpanelflowlayoutpanel

Adding multiple buttons to tablelayoutpanel.flowlayoutpanel based on data retrieved from database


I have a tablelayoutpanel with a flowlayoutpanel in each cell so that I can add multiple buttons to these cells. I now want to place a button in the cell based on data retrieved from a database.

E.g.

enter image description here enter image description here

3 buttons would be added to secondary 2 bimester 2 cell. The flowlayoutpanel in this cell is named "S2B2_LayoutPanel". Others are named in the same format and the data from the database wont always say S2 2.. it could be any year or bimester.

I need something like:

string year = String.Format("{0}", reader["Year"]);
string bimester = String.Format("{0}", reader["Bimester"]);

string CellLocation = year+"B"+bimester+"_LayoutPanel";

Button btn = new Button();
btn.Text = String.Format("{0}", reader["Title"]);

year + "B" + bimester + "_LayoutPanel".Controls.Add(btn, 0, 0);
//or
this.@"CellLocation".Controls.Add(btn,0,0);

Solution

  • You can get the FlowLayoutPanel from the TableLayoutPanel's Controls collection by name:

    var flp = (FlowLayoutPanel) myTableLayoutPanel.Controls[year + "B" + bimester + "_LayoutPanel"];