根据表列名自动创建textBox

 private void InitLabTxt()
    {
        label1.Font = new Font("微软雅黑", 20);
        comboBoxPname.Font = new Font("微软雅黑", 15);
        comboBoxPname.Size = new Size(145, 120);
        comboBoxPname.Location = new Point(150, 20);
        if (true)
        {
            listLB.Clear();
            listTB.Clear();
            //  groupBox1.Controls.Clear();
            groupBox1.Width = this.Width;
            WeiShiJiConfig wsj = new WeiShiJiConfig();

            DataTable dt = wsj.getConfig();
            for (int i = 2; i < dt.Columns.Count-1; i++)
            {
                if (i < 5)
                {
                    Label label = new Label() { Text = dt.Columns[i].ColumnName, TextAlign = ContentAlignment.MiddleRight, Font = new Font("微软雅黑", 20), Size = new Size(150, 70), Location = new Point(305 * (i-2)+300 , 20) };
                    listLB.Add(label);

                    TextBox textBox = new TextBox() { Name = dt.Columns[i].ColumnName, Font = new Font("微软雅黑", 25), Size = new Size(145, 120), Location = new Point(305 * (i-2)+450 , 20) };
                    listTB.Add(textBox);
                }
                else if (i < 10)
                {
                    Label label = new Label() { Text = dt.Columns[i].ColumnName, TextAlign = ContentAlignment.MiddleRight, Font = new Font("微软雅黑", 20), Size = new Size(150, 70), Location = new Point(305 * (i - 5), 100) };
                    listLB.Add(label);
                    //Text = dt.Rows[i]["projectColName"].ToString(),
                    TextBox textBox = new TextBox() { Name = dt.Columns[i].ColumnName, Font = new Font("微软雅黑", 25), Size = new Size(145, 120), Location = new Point(305 * (i - 5) + 150, 100) };
                    listTB.Add(textBox);
                }
                else if (i < 15)
                {
                    Label label = new Label() { Text = dt.Columns[i].ColumnName, TextAlign = ContentAlignment.MiddleRight, Font = new Font("微软雅黑", 20), Size = new Size(150, 70), Location = new Point(305 * (i - 10), 180) };
                    listLB.Add(label);

                    TextBox textBox = new TextBox() { Name = dt.Columns[i].ColumnName, Font = new Font("微软雅黑", 25), Size = new Size(145, 120), Location = new Point(305 * (i - 10) + 150, 180) };
                    listTB.Add(textBox);
                }
                else
                {
                    Label label = new Label() { Text = dt.Columns[i].ColumnName, TextAlign = ContentAlignment.MiddleRight, Font = new Font("微软雅黑", 20), Size = new Size(150, 70), Location = new Point(305 * (i - 15), 260) };
                    listLB.Add(label);

                    TextBox textBox = new TextBox() { Name = dt.Columns[i].ColumnName, Font = new Font("微软雅黑", 25), Size = new Size(145, 120), Location = new Point(305 * (i - 15) + 150, 260) };
                    listTB.Add(textBox);
                }

            }
            groupBox1.Controls.AddRange(listLB.ToArray());
            groupBox1.Controls.AddRange(listTB.ToArray());
        }
    }

/**********************************/

string colnames = "pName,recordTime";
string values = "'" + comboBoxPname.Text.Trim() + "'" + "," + "'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'";
foreach (TextBox text in listTB)
{
if (text.Text.Length == 0)
text.Text = "0";
colnames += "," + text.Name;
values += "," + "'" + text.Text + "'";
}
WeiShiJiConfig productQuality = new WeiShiJiConfig();
productQuality.pName = comboBoxPname.Text.Trim();
string result = productQuality.Add(colnames, values);

你可能感兴趣的:(根据表列名自动创建textBox)