TableLayoutPanel 常见操作

本文探讨了在 WinForms 2.0 应用中使用 TableLayoutPanel 控件的方法,包括如何动态添加和删除行及列,以及调整单元格大小等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 'm using the the TableLayoutPanel for a new WinForms 2.0 app and love the way it mimics HTML tables in terms of layout, automatic resizing etc.... But, incredibly, there seems to be no way to dynamically add or delete rows from this container at runtime. Has anyone figured out how to do this? What would be the technical/architectural argument for Microsoft's not exposing Rows and Columns collections in this control that can be managed programmatically? IMO....without this seemingly basic feature, the potential uses for this control diminish greatly.
Mike Morris Send private email
Friday, January 06, 2006
Have you tried MyTableLayoutPanel.Controls.Add(control,colnum,rownum) ?
GiorgioG Send private email
Friday, January 06, 2006
Anything that can be done at design time can also be done at run time. Look at the InitializeComponent method for clues.
Turtle Rustler
Friday, January 06, 2006
Hi,
  to add rows and columns dynamical simply set the column or row size!  You then have to add specific attributes for the columns and rows based using the ColumnStyles and RowStyles properties.  The code below illustrates:

table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            table.Width = 210;
            table.Height = 210;
            table.RowCount = 20;
            table.ColumnCount = 20;
            for (int i = 0; i < 20; i++)
            {
                table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 10));
                table.RowStyles.Add(new RowStyle(SizeType.Absolute, 10));
            }
Jerod Edward Moemeka Send private email
Tuesday, January 10, 2006
Hey I just got the "delete" row working.  You have to delete the controls in the row first then shift all the other controls up by one row. Try this to see if it will work for you. FYI I am using the tag to mark the control row id.  Example:

protected void DeleteButton_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Line item delete pressed. Tag = " + ((Button)sender).Tag);
            int iRowId = int.Parse(((Button)sender).Tag.ToString());

            this.tlpBillItem.SuspendLayout();

            Control c = null;

            //delete current row controls
            for (int j = 0; j < this.tlpBillItem.ColumnCount; j++)
            {
                c = this.tlpBillItem.GetControlFromPosition(j, iRowId);
                if (c != null)
                {
                    this.tlpBillItem.Controls.Remove(c);
                }
            }

            //need to shift all controls up one row from delete point
            TableLayoutPanelCellPosition controlPosition;
            for (int i = iRowId; i < this.tlpBillItem.RowCount; i++)
            {
                for (int j = 0; j < this.tlpBillItem.ColumnCount; j++)
                {
                    c = this.tlpBillItem.GetControlFromPosition(j, i + 1);
                    if (c == null)
                        break;
                    c.Tag = i;
                    controlPosition = new TableLayoutPanelCellPosition(j, i);
                    this.tlpBillItem.SetCellPosition(c, controlPosition);
                }
            }

            //need to remove the row
            this.tlpBillItem.RowCount--;

            this.tlpBillItem.ResumeLayout(false);
            this.tlpBillItem.PerformLayout();
        }
Tony Nguyen Send private email
Friday, January 20, 2006
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值