简体   繁体   中英

Insert Row in Middle of Table Layout panel C# Windows

I am trying to add a row in middle of table layout panel. However I am struggling to find a way to do this. I tried following from one of the article on internet but this doesn't seems to be working. Can you help.

i have 3 columns and 5 rows in table layout panel. each row containing lable, textbox and blank lable.

i am trying to add a row after 2nd row in already created table layout panel.

var AddOnControl = ConfirmationTable.Controls.Find("Discount", true).First();
int childIndex = 1+ ConfirmationTable.Controls.GetChildIndex(AddOnControl);


Label lbl = new Label();
lbl.Name = key;
lbl.Text = key;
lbl.Font = new Font("Calibri", 10F, System.Drawing.FontStyle.Regular);
lbl.Size = new Size(lbl.Size.Width + 70, lbl.Size.Height);

ConfirmationTable.Controls.Add(lbl);

TextBox txt = new TextBox();
txt.Multiline = true;
txt.TextChanged += txt_TextChanged;
txt.Name = "txtPrice" + key;
txt.Text = value;
txt.BorderStyle = BorderStyle.None;

ConfirmationTable.Controls.Add(txt);

lbl = new Label();
lbl.Name = "blank";
ConfirmationTable.Controls.Add(lbl);

ConfirmationTable.Controls.SetChildIndex(lbl, childIndex);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 1);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 2);

but above code always adds a row at the bottom of the table. Any suggestion?

There is an overload of the Controls.Add method that accepts two integers as indexes for column/row position.

Like this:

tableLayoutPanel1.Controls.Add(lb3, 0, 1);

I believe this is enough to solve your problem in a simple way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM