如何在运行时动态创建文本框并从mysql c#显示数据文本框?

yizd12fk  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(296)
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);
           }
dgsult0t

dgsult0t1#

试试这个:

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";
       }    
    }

希望这有帮助!

q5iwbnjs

q5iwbnjs2#

假设您将从mysql检索到的数据放在一个具有propertis id和name的对象列表中,那么您将对它们进行循环,并创建一个textbox并将其添加到表单中:

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);
}

相关问题