0

I have to build some data storing application for my office works. With this application I planned to store MAC address and Windows licensee Keys in Mysql database. I decided to store MAC address and windows keys encrypted rather than plain text. To do this I use this encryption method

C# encryption

My problem is when I finally decrypt the data in data-grid view the it start to lag so badly. Even with the populating data as well as scrolling.

this is a code that I used to decrypt data-grid view data.

private void pc_count_grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                e.Value = Decrypt(e.Value.ToString());
            }

            if (e.ColumnIndex == 4)
            {
                e.Value = Decrypt(e.Value.ToString());
            }

        }

How can I improve performance of the data-grid view while decrypting the data in cell formatting ?

Community
  • 1
  • 1
rafalefighter
  • 714
  • 2
  • 11
  • 39
  • "CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event." from MSDN. therefore the answer for "How can I improve performance of the data-grid view?" question is "Don't use CellFormatting event. There must be other ways" – ASh Aug 15 '16 at 14:43

1 Answers1

0

The CellFormatting event will fire quite often, so your decryption method will be called repeatedly, sometimes for the same data. This will cause the rendering to appear slow. You should decrypt the values while retrieving them from the data store, then bind to the DGV.

From MSDN, "The CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event."

Pseudocode:

while (r = GetNExtRecordFromMySQL())
{
    Foo f = new Foo(r);
    f.DecryptedField1 = Decrypt(f.Feild1);
    f.DecryptedField2 = Decrypt(f.Field2);
    .
    .
    .
}

// more code

dgv1.Columns[0].Name = "Field1";
dgv1.Columns[0].DataPropertyName = "DecryptedField1";
dgv1.Columns[1].Name = "Field2";
dgv1.Columns[1].DataPropertyName = "DecryptedField2";
cdkMoose
  • 1,646
  • 19
  • 21
  • How can I decrypt the every single value before bind them to DGV? I would really thankful if you can point me to some tutorials . – rafalefighter Aug 15 '16 at 16:20
  • As you read the data out of MySQL, convert the decrypted fields and store them in your business object. Then bind the decrypted fields to the DGV columns instead of the database fields. – cdkMoose Aug 15 '16 at 17:35