0

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
maam27
  • 444
  • 3
  • 21
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Sinatr Mar 05 '15 at 13:34
  • the question i have is not why isnt it working but how would i go about adding column span to the label if i create the label in code like the example above. – maam27 Mar 05 '15 at 13:58

4 Answers4

1

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");
TaW
  • 53,122
  • 8
  • 69
  • 111
  • i tried to set the name my self as wel but i gues i did something wrong with the code since it wouldn't recognize the name of the control later on in the code. but i have something that works for now. but still thanks for trying to help – maam27 Mar 05 '15 at 15:20
1

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);    
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

Set the ColumnSpan property of the label.

IglooGreg
  • 147
  • 5
  • I know i need to set that property the question I have is how do i set the columnspan on that label. – maam27 Mar 05 '15 at 13:57
  • As you have now worked out, you have to know what the ID of the label is. So as per your example below, you have var label = new Label(), you can use label.ColumnSpan = 2, then add the label to the panel controls. – IglooGreg Mar 05 '15 at 15:54
0

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); 
maam27
  • 444
  • 3
  • 21