1

I have a method Load in MyController:

public void Load(ItemsControl control, string commandText)
{
    try
    {
        _db.OpenConnection();

        using (SQLiteCommand command = new SQLiteCommand(commandText, _db.Connection))
        using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command))
        {
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            control.ItemsSource = dataTable.AsDataView();
        }
    }
    catch (Exception exp)
    {
        //...;
    }
}

Then in XAML I have:

<DataGrid x:Name="DataGrid_1" ... AutoGenerateColumns="True" Loaded="DataGrid_1_Loaded">

and finally in DataGrid_1_Loaded() I'm calling the method Load() like this:

MyController.Load(DataGrid_1, "CREATE VIEW IF NOT EXISTS content_for_dg AS SELECT name,surname FROM people");

When I run the program, I see a little white space at the top of Data_Grid_1 but no columns and no data. I opened the database with SQLite Browser and the view content_for_dg exists and contains correct data...

What am I missing ? Why the data are not shown in DataGrid ?

2
  • I tested the code, and it worked as expected. Note that I created the data table manually for testing. Can you put a breakpoint on the line where you set control.ItemsSource and verify that the data table does in fact contain data? Commented May 15, 2018 at 17:43
  • @RyanS thanks for answering. You are right - dataTable.DataSet = null at breakpoint... Commented May 15, 2018 at 19:27

1 Answer 1

1

If I remove using (SQLiteCommand command = new SQLiteCommand(commandText, _db.Connection)) and create SQLiteDataAdapter this way SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(commandText, _db.Connection), then it works fine...

public void Load(ItemsControl control, string commandText)
{
    try
    {
        _db.OpenConnection();

        using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(commandText, _db.Connection))
        {
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            control.ItemsSource = dataTable.AsDataView();
        }
    }
    catch (Exception exp)
    {
        //...;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.