Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在运行时动态创建文本框并从MYSQL C中显示数据文本框?_C#_Mysql - Fatal编程技术网

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

C# 如何在运行时动态创建文本框并从MYSQL C中显示数据文本框?,c#,mysql,C#,Mysql,假设您将从MySql检索到的数据放在一个具有属性ID和名称的对象列表中,那么您将循环它们并创建一个文本框并将其添加到表单中: private void button1_Click(object sender, EventArgs e) { try { string MyConnection2 = "datasource = 127.0.0.1;port=3306;username = root;passw

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

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);
           }
试试这个:

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

希望这有帮助

请显示最小、完整、,到目前为止您尝试过的可验证示例->我希望在运行时动态创建多个文本框并根据特定ID显示MySQL表中的数据我如何赋值动态文本框我不理解…查看代码并给出解决方案谢谢发布代码请查看并更正代码…谢谢现在请查看如何分配值动态文本框我不明白…请检查代码并给我解决方案谢谢您是指我如何分配值动态文本框
 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";
       }    
    }