我有TableLayoutPanel并动态添加行。如何在特定索引处插入带有控件的行?
private void AddRowstoTableLayout()
{
for (int cnt = 0; cnt < 5; cnt++)
{
RowStyle newRowStyle = new RowStyle();
newRowStyle.Height = 50;
newRowStyle.SizeType = SizeType.Absolute;
Label lbl1 = new Label();
lbl1.Text = "label-" + (cnt + 1);
TextBox t1 = new TextBox();
t1.Text = "text-" + (cnt + 1);
Label lblMove = new Label();
lblMove.Text = "Move-" + (cnt + 1);
lblMove.MouseDown += new MouseEventHandler(dy_MouseDown);
tableLayoutPanel1.RowStyles.Insert(0, newRowStyle);
this.tableLayoutPanel1.Controls.Add(lbl1, 0, cnt); //correct
this.tableLayoutPanel1.Controls.Add(t1, 1, cnt); //correct
this.tableLayoutPanel1.Controls.Add(lblMove, 2, cnt); //correct
tableLayoutPanel1.RowCount += 1;
}
}
我尝试过
tableLayoutPanel1.RowStyles.Insert(0, newRowStyle);
但没有运气
答案 0 :(得分:0)
没有开箱即用的此类功能。 方法是在TableLayoutPanel中添加一行。然后,将位于大于等于所需行索引的行中的所有控件移至高于当前位置的rowindex中。 这里是您如何做到的:
void InsertTableLayoutPanelRow(int index)
{
RowStyle newRowStyle = new RowStyle();
newRowStyle.Height = 50;
newRowStyle.SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles.Insert(index, newRowStyle);
tableLayoutPanel1.RowCount++;
foreach (Control control in tableLayoutPanel1.Controls)
{
if (tableLayoutPanel1.GetRow(control) >= index)
{
tableLayoutPanel1.SetRow(control, tableLayoutPanel1.GetRow(control) + 1);
}
}
}