TableLayoutPanel来动态添加表格

TableLayoutPanel来动态添加表格

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace addTableElement
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tableP1.ColumnCount = 1;
            tableP1.Dock = DockStyle.Fill;
            tableP1.AutoScroll = true;
            tableP1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
     

        }


        public static void addTableElement(TableLayoutPanel tbp, string text1)
        {
            // 动态添加一行
            tbp.RowCount++;

            //设置高度
            //table.Height = splitContainer2.Height;
            // 行高
            tbp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
            // 设置cell样式,
            //table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
           TextBox tx = new TextBox();
            tbp.Controls.Add(tx, 0, tbp.RowCount - 1);

        }
        public static void addTableElement(TableLayoutPanel tbp)
        {
            // 动态添加一行
            tbp.RowCount++;
            tbp.ColumnCount = 5;
            //设置高度
            //table.Height = splitContainer2.Height;
            // 行高
            tbp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
            tbp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, tbp.Width * 0.2f));//列宽为1/5,一共添加5个
            tbp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, tbp.Width * 0.2f));
            tbp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, tbp.Width * 0.2f));
            tbp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, tbp.Width * 0.2f));
            tbp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, tbp.Width * 0.2f));
            // 设置cell样式,
            //  tbp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            TextBox tp = new TextBox();
            // tp.Dock = DockStyle.Fill;
            TextBox tp1 = new TextBox();
            TextBox tp2 = new TextBox();
            TextBox tp3 = new TextBox();
            TextBox tp4 = new TextBox();
            TextBox tp5 = new TextBox();
            tbp.Controls.Add(tp,0, tbp.RowCount - 1);
            tbp.Controls.Add(tp1, 1, tbp.RowCount - 1);
            tbp.Controls.Add(tp2, 2, tbp.RowCount - 1);
            tbp.Controls.Add(tp3, 3, tbp.RowCount - 1);
            tbp.Controls.Add(tp4, 4, tbp.RowCount - 1);
        }
        public static TableLayoutPanel tableP1 = new TableLayoutPanel();
        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Controls.Add(tableP1);
            addTableElement(tableP1);
        }
    }
}

页面:控件panel1和button

页面图片

TableLayoutPanel来动态添加表格