Search code examples
c#wpfwpfdatagrid

Displaying data on a datagrid too slow


I have a datagrid (WPF/C#), that's pulling in two fields from a rather large MySQL view. However, it takes far too long to render onto the screen and I wanted to know if there is a quicker way?

private void SetupDataGrid()
{
    try
    {
        _con.Open();
        var com = new MySqlCommand("SELECT `Record ID`, `Company Name` FROM tblTest.all;") { Connection = _con, CommandType = CommandType.Text };
        var dt = new DataTable();
        var sdt = new MySqlDataAdapter(com);
        sdt.Fill(dt);
        DataGridActivities.DataContext = dt;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    _con.Close();
}

Solution

  • You could introduce paging into the grid, and only extract from the database the records which you are going to display. Another approach would be to extract data, cache it, and then access the cache - but in this case first time round would still be slow.

    If extracting a large data set is the problem, I would consider only extracting the items for the current page.

    With MySQL you can do this in the following approach

    SELECT * FROM [TABLE] LIMIT 5,10;