我如何在运行时动态创建文本框并显示数据文本框形式MYSQL C#?

时间:2018-06-07 06:58:49

标签: c# mysql

private void button1_Click(object sender, EventArgs e)
       {
           try
           {
                 string MyConnection2 = "datasource = 127.0.0.1;port=3306;username = root;password =; database = test123; SslMode=None ;Convert Zero Datetime=True";
                 int txtno = int.Parse(textBox1.Text);
                 int pointX = 30;
                 int pointY = 40;
                 panel2.Controls.Clear();
                 for (int i = 0; i < txtno; i++)
                 {
                     TextBox a = new TextBox();
                     a.Text = (i + 1).ToString();
                     a.Location = new Point(pointX, pointY);
                     panel2.Controls.Add(a);
                     panel2.Show();
                     pointY += 20;
                 }
                 string Query = "select task_comment from test123.task_comment  where task_id ='" + textBox1.Text + "'";
                 MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                 MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                 MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
                 MyAdapter.SelectCommand = MyCommand2;
                 MyConn2.Close();
                 MySqlDataReader MyReader2;
                 MyConn2.Open();
                 MyReader2 = MyCommand2.ExecuteReader();
                 while (MyReader2.Read())
                 {
                       txtno = Convert.ToInt32(MyReader2["task_comment"].ToString());
                 }
                 MyConn2.Close(); //Connection closed here 
           }
           catch (Exception ex)
           {
                MessageBox.Show(ex.Message);
           }

2 个答案:

答案 0 :(得分:0)

假设您在具有属性ID和名称的对象列表中从MySql检索到数据,因此您循环遍历它们并创建一个文本框并将其添加到表单中:

    MySqlDataReader MyReader2;
    MyConn2.Open();

    MyReader2 = MyCommand2.ExecuteReader();
    while (MyReader2.Read())
    {
        txtno = Convert.ToInt32(MyReader2["task_comment"].ToString());
        TextBox bx = new TextBox();
        bx.Text = txtno;
        // retrieve here any other data u need to assign to the textbox

        bx.Location = new Point(x, y);// x and y should be the calculated //coordinates, this will depend on how you want to display the textboxes
        this.Controls.Add(bx);// Add the textbox to the form

    }
    MyConn2.Close(); //Connection closed here 
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

答案 1 :(得分:0)

试试这个:

 protected void Page_Load(object sender, EventArgs e)
    {
       int n = 4;//fetch your rows from database in datatable, then count number of rows. And based on those 
       //count create textboxes.
       TextBox[] textBoxes = new TextBox[n];
       for (int i = 0; i < n; i++)
       {
        textBoxes[i] = new TextBox();
        // Here you can modify the value of the textbox which is at textBoxes[i]
        textBoxes[i].Text="Your text from database";
       }    
    }

希望这有帮助!