A
A
Anton_repr2019-10-26 13:39:23
WPF
Anton_repr, 2019-10-26 13:39:23

How to retrieve the correct data from the database?

I connected the database to the application, brought something to the data grid.
"binding" to datagrid headers does not work. (the binding is in quotes, because it's not a binding).
Three additional columns are created, how to remove them?
Connection and output:

string MyConString = "server=localhost;user=root;database=food;password=root;";
            string sql = "SELECT name, amount, date FROM Products";

            using (MySqlConnection connection = new MySqlConnection(MyConString))
            {
                try
                {
                    connection.Open();
                    using (MySqlCommand cmdSel = new MySqlCommand(sql, connection))
                    {
                        DataTable dataTable = new DataTable();
                        MySqlDataAdapter adapter = new MySqlDataAdapter(cmdSel);
                        adapter.Fill(dataTable);
                        productsgrid.ItemsSource = dataTable.DefaultView;
                    }
                    connection.Close();
                }
                catch(Exception)
                {
                    MessageBox.Show("Something wrong");
                }
            }

I just call this method after InitializeComponent();
everything looks like this:
5db422168daa9660976587.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton_repr, 2019-10-26
@Anton_repr

It was necessary to set AutoGenerateColumns to False (as Foggy Finder said ) and then set the binding for the columns -Binding="{Binding Path='value'}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question