0

I have to display every row from table choose by the user, everyting seems be good, but the table is filled by blank rows, I am using DataGrid to display data. Can anyone tell me what I am doing wrong here ?

private void showSelectedTable(object sender, RoutedEventArgs e)
    {
        if (mysql.IsChecked == true)
        {
            string query = "SELECT * FROM " + tableListBox.SelectedItem.ToString();
            MySqlCommand myTableContent = new MySqlCommand(query, (MySqlConnection)conn);
            try
            {
                MySqlDataReader myReader = null;
                myReader = myTableContent.ExecuteReader();
                choosenDataTable = myReader.GetSchemaTable();
                foreach (DataRow row in choosenDataTable.Rows)
                {
                    DataGridTextColumn textColumn = new DataGridTextColumn();
                    textColumn.Header = row.Field<String>("ColumnName");
                    tableDataGrid.Columns.Add(textColumn);
                }

                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        ObservableCollection<String> observable = new ObservableCollection<String>();
                        for (int i = 1; i < myReader.Columns.Count; i++)
                        {
                            observable.Add(myReader.GetString(i));

                        }
                        tableDataGrid.Items.Add(observable);
                    }
                }

            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }


        }
    }

My app windows, with "filled" DataGrid

Blvckhype
  • 17
  • 1
  • WPF app, and there is DataGrid – Blvckhype May 23 '18 at 18:23
  • OP, the answer seems to be to copy the MySqlDataReader data into a DataTable. There's nothing difficult about it, but let me know if you run into any snags. You'll be adding columns to the DataTable, not the DataGrid -- then if you like you can just let the Data auto-generate its own columns based on the DataTable. – 15ee8f99-57ff-4f92-890c-b56153 May 23 '18 at 18:37

1 Answers1

0

You can divide your problem in parts

  1. Generate Table dynamically from Data Source
  2. Attach that dynamic Data Source to Gridview

But I think This will solve your both issues and this is something you want

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • My problem is almost solved, this code is working, but i need only help with input data to DataGrid, because I get my data, but when I want add it i get blank rows – Blvckhype May 23 '18 at 18:24
  • Can you update the code with what you tried? – Mihir Dave May 23 '18 at 18:26
  • There were a lot of attempts, now I updated the code to up to date version, this code is working, its getting values from every cell and put it in var observable, but don't fill the DataGrid (only create blank cells and rows) – Blvckhype May 23 '18 at 19:15
  • @Blvckhype No observablecollction. Don't use observablecollection. DataGrid cannot use observablecollection for row. Cannot. Instead fill DataTable like his linked example suggests. His example is winforms so you want to use DataGrid.ItemsSource instead of `DataSource` but it's `DataTable` that is important to use. – 15ee8f99-57ff-4f92-890c-b56153 May 23 '18 at 20:25