Reputation: 434
I'm trying to make a dynamic layout to display data from a database, for that i'm using a tablelayoutpanel and I got it to where it puts the data in the fields I want it go to but I can't figure out how I would go about giving some of the fields a row or column span since some of them need to go over multiple fields
this is the code I use to create one of the labels that would need a columnspan
tableLayoutPanel1.Controls.Add(new Label()
{ Text = stat, Dock = DockStyle.Fill, BackColor = Color.Green, AutoSize = true }
, 7, row);
I did search on SO for a solution and I did find some that would set the columnspan, but they just wouldn't work with the way i create the labels.
tableLayoutPanel1.SetRowSpan([control name],[rowspan] );
//[] is what is supposed to be placed there
since this is the code I found and because I created the labels in the code I can't give the control name. (It's very possible due to a mistake made by me)
Upvotes: 0
Views: 2398
Reputation: 21999
Add methods are typically return void
. If you want to operate with the item, then it make sense to either do it before calling Add()
(construct complete object) or create an instance in the normal way
var item = new Label() { Text = stat, Dock = DockStyle.Fill, BackColor = Color.Green, AutoSize = true };
// do something with item here
tableLayoutPanel1.Controls.Add(item, 7, row);
// or do something with item here, e.g.:
tableLayoutPanel1.SetColumnSpan(item, 2);
Upvotes: 1
Reputation: 54433
Your original code will work if you can add an identifying Name
property:
tableLayoutPanel1.Controls.Add(new Label()
{ Name = "L1", Text = stat, Dock = DockStyle.Fill,
BackColor = Color.Green, AutoSize = true }, 7, row);
Now you can use that Name
:
tableLayoutPanel1.SetRowSpan(tableLayoutPanel1.Controls["L1"], 2 );
To create the Name
you could use the Controls.Count
property:
string name = "L" + tableLayoutPanel1.Controls.Count.ToString("00");
Upvotes: 1
Reputation: 434
After some more searching i found a way to do it. instead of trying to do it like this:
tableLayoutPanel1.Controls.Add(new Label()
{ Text = naam, Dock = DockStyle.Fill, BackColor = Color.Green, AutoSize = true }
, 2, row);
im now creating the labels that need columnspan like this
var label = new Label();
label.Text = naam;
label.Dock = DockStyle.Fill;
label.BackColor = Color.Red;
label.AutoSize = true;
tableLayoutPanel1.Controls.Add(label);
tableLayoutPanel1.SetCellPosition(label, new TableLayoutPanelCellPosition(2, row));
tableLayoutPanel1.SetColumnSpan(label, 2);
Upvotes: 0