0

I recently developed an app that has a datagridview on it. After a couple minutes of testing i noticed that the performance is very bad and there are issues while rendering data(lag,slow manipulation) i was also searching for solutions like enabling virtual mode and so on ... to no avail, can you please suggest or help me is there a way i can enhance the performance and mode this code.

So here is my code:

private void LoadTable()
    {
        var connection = Connection.prevzemiKonekcija();

        var adapter1 = new MySqlDataAdapter();
        var sqlSelectAll = "SELECT * from prodavnica.artikli";
        adapter1.SelectCommand = new MySqlCommand(sqlSelectAll, connection);

        var table = new DataTable();
        adapter1.Fill(table);

        var bajndsors = new BindingSource();
        bajndsors.DataSource = table;


        dataGridView1.DataSource = bajndsors;

        dataGridView1.RowsDefaultCellStyle.BackColor = Color.Linen;
        dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
            Color.Cornsilk;


        /*
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        dataGridView1.AutoResizeColumns();


        dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
        dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
        dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

        dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
        dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

*/


        dataGridView1.Columns[0].HeaderText = "РЕД.БР";
        dataGridView1.Columns[1].HeaderText = "ШИФРА";
        dataGridView1.Columns[2].HeaderText = "НАЗИВ";
        dataGridView1.Columns[3].HeaderText = "НАБАВНА ЦЕНА";
        dataGridView1.Columns[4].HeaderText = "ПРОДАЖНА ЦЕНА";
        dataGridView1.Columns[5].HeaderText = "КОЛИЧИНА";
        dataGridView1.Columns[6].HeaderText = "ДАНОК";
        dataGridView1.Columns[7].HeaderText = "ОПИС";
        dataGridView1.Columns[8].HeaderText = "ДОЛГ ОПИС";
        dataGridView1.Columns[9].Visible = false;
        dataGridView1.Columns[10].HeaderText = "ЕДИНИЦА";
        dataGridView1.Columns[11].HeaderText = "ПРОФИТ";
        dataGridView1.Columns[12].HeaderText = "ПРОИЗВОДОТЕЛ";

            this.dataGridView1.VirtualMode = true;

            //dataGridView1.Columns["MakArtikal"].Visible = false;


                    connection.Close();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Timo Dimce
  • 21
  • 5
  • How many rows are we displaying? – LarsTech Dec 06 '19 at 23:45
  • The table has 54.320 records on it. So i am thinking how many should i display and is there a way i can load them all. – Timo Dimce Dec 06 '19 at 23:46
  • 1
    The usual answer is: does the user need to see 54K records? How many columns are you displaying? Do you need to display all of them? Why didn't VirtualMode work? – LarsTech Dec 06 '19 at 23:52
  • The records are information about products so the `datagridview` is being filtered by a texboxt.So the answer is YES all of the records need to be present. The virutalmode is as you can see enabled and there is no result however. – Timo Dimce Dec 06 '19 at 23:54
  • See the second bullet point in this answer: [Best way to fill DataGridView with large amount of data](https://stackoverflow.com/a/3580421/719186) – LarsTech Dec 06 '19 at 23:56
  • What is it , Bulgarian? When you do `select` in your sql, you can do it in such way that you have one virtual column - `rownumber`. Then you can easy page number of rows in your grid using `DataView` – T.S. Dec 07 '19 at 02:28
  • Don't use `dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells`. There is no point to size based on non visible cells. If you must have auto sizing use the `DataGridViewAutoSizeColumnsMode.DisplayedCells` option. You enabled virtual mode yet still set a datasource instead of handling the CellValueNeeded event; have you researched how to do virtual-mode? – TnTinMn Dec 07 '19 at 03:06
  • You should try to [DoubleBuffer the DGV](https://stackoverflow.com/questions/44185298/update-datagridview-very-frequently/44188565#44188565). – TaW Dec 07 '19 at 08:37
  • Try moving the `dataGridView1.DataSource = bajndsors;` to the end of the method. Also, check you aren't calling the `LoadTable()` method over and over. – SSS Dec 09 '19 at 01:04

1 Answers1

0

Using the SqlDataReader is way faster, check https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

Gary B
  • 17
  • 3