It is necessary on request from the textbox to display a table from MySql. I assume a conclusion in DataGrid, but I do not understand how to implement it.

The datagrid accepts a typed collection. However, it is not possible to create a class for the table object, since adding new fields is not excluded.

That could squeeze out:

string connstr = "server=localhost; user=root; database=lab_3; password=0000"; using (MySqlConnection conn = new MySqlConnection(connstr)) { conn.Open(); string query = "SELECT * FROM new_table;"; MySqlCommand command = new MySqlCommand(query, conn); MySqlDataReader reader = command.ExecuteReader(); ArrayList arrayList = new ArrayList(); if (reader.HasRows) { while (reader.Read()) { int index = 0; arrayList.Add(reader[index]); } grid.ItemsSource = arrayList; reader.Close(); } 

I will be glad to any suggestions and ideas!

  • DataGrid takes a typed collection - where did you get it? Like you can bind and DataTable - Andrew NOP
  • Divide the question in two: how to read data from the database, and how to display data using WPF. Which one of the items you fail ? - Mikhail Ionkin
  • @MikhailIonkin Cannot display data in WPF. There seems to be no problem with reading, but I'm not sure that my way of reading data is right for this situation. Perhaps there is another way by which you can present the data immediately in the required or more convenient for parsing form, but I have not found one. - raspberry367

1 answer 1

Something like this:

 DataTable dt = new DataTable(); dt.Load(reader); datagrid.AutoGenerateColumns = true; datagrid.ItemsSource = dt.DefaultView;