When getting a record from a table, I get only one record in Lable How can I make other Lable generated by a record in a table?

code below

  MySqlConnection con = new MySqlConnection(ConString.ToString()); string cmdString = "SELECT * FROM mon ORDER BY `Name_PC` + 0"; MySqlCommand cmd = new MySqlCommand(cmdString, con); con.Open(); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Label label = new Label(); label.Text = reader["Name_PC"].ToString(); label.BackColor = Color.Orange; label.BorderStyle = BorderStyle.Fixed3D; this.Controls.Add(label); } con.Close(); 

    3 answers 3

    Good day, the fact is that with such program execution, the label will display only the latest data read from the table, in order to display all the data, create either a datatable or an array from the label, or create an array variable and put everything into the array, and after Print in the label the data you need. Good luck

    Another datagridview is suitable for your task. And if not, then try to fill the array with your data and then output the necessary values ​​in your label.

    • datagridview does not fit - Paul

    In general, everything turned out to be much simpler.

      MySqlConnection con = new MySqlConnection(ConString.ToString()); string cmdString = "SELECT * FROM mon ORDER BY `Name_PC` + 0"; MySqlCommand cmd = new MySqlCommand(cmdString, con); con.Open(); MySqlDataReader reader = cmd.ExecuteReader(); var i = 0; while (reader.Read()) { Label label = new Label(); label.Text = reader["Name_PC"].ToString(); label.BackColor = Color.Orange; label.Location = new Point(20, 25 * (i++)); label.BorderStyle = BorderStyle.Fixed3D; this.Controls.Add(label); } con.Close(); 

      It is even easier to use the assignment "+ =" and at the end of the Text line add the Line Feed "\ n"

       label.Text += reader["Name_PC"].ToString() + "\n";