TableLayoutPanel 常见操作

来源:互联网 发布:淘宝交易纠纷处理 编辑:程序博客网 时间:2024/05/21 09:47
 '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
原创粉丝点击