DataGridView Performance issues with CellFormatting

Hello:

I'm using the DataGridView for displaying data to the user. The number of records that will be displayed at a certain time could vary between 0 and 150. I use the CellFormatting event handler to do some formatting like changing the font face and the background colors based on the cell values.

Earlier we were iterating through the rows of the gridview to accomplish the same. Since the performance was dreadful, I changed the code to use the CellFormatting. Now, the performance when the gridview loads is a lot faster than the earlier approach. But everytime, we scroll through the gridview the application just hangs, and its becoming terrible even for a gridview of 50 rows.

Is this a bug with the DataGridView control I'm using the Visual Studio .NET 2005 RTM version 2.0.50727. I had seen problems of the same nature in a few other postings, but those were related to the beta versions.

TIA.

Regards,

Skandy.




Answer this question

DataGridView Performance issues with CellFormatting

  • ofireps

    Hi Mark:

    Thanks for looking into this.

    The following is the code that I've in the CellFormatting event:

    public void objGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

    {

    bool blnStaffMember = true;

    bool blnInactive = false;

    bool blnDiscontinued = false;

    try

    {

    blnStaffMember =

    (bool)this.objDSM.GetType().GetMethod("IsStaffMember").Invoke(this.objDSM, new object[] {

    this.GetSourceValue(e.RowIndex,"UserTicket").ToString()});

    }

    catch (System.Exception ee) { };

    try

    {

    blnInactive = (bool)this.GetSourceValue(e.RowIndex, "INACTIVE", sender);

    }

    catch (System.Exception ee) { };

    try

    {

    blnDiscontinued = (bool)this.GetSourceValue(e.RowIndex, "DISCONTINUED", sender);

    }

    catch (System.Exception ee) { };

    try

    {

    e.CellStyle.Font = new Font(FontFamily.GenericSansSerif, 8, (!blnStaffMember

    FontStyle.Italic : FontStyle.Regular) | (blnInactive FontStyle.Strikeout : FontStyle.Regular));

    if (blnStaffMember)

    e.CellStyle.ForeColor = Color.Black;

    if (blnDiscontinued)

    {

    e.CellStyle.ForeColor = Color.Red;

    e.CellStyle.BackColor = Color.Violet;

    }

    }

    catch (System.Exception ee) { };

    }

    And this is a SingleThreaded application! And as I had mentioned earlier the gridview in any of the problem instances doesnt hold more than 50 rows of data.

    Thanks again!

    Skandy.



  • Quint

    Hi Mark:

    I was trying to see if there was any more data that I can offer you that might be useful in your investigation.

    I downloaded the Debugging Tools for Windows and took multiple hang dumps and found that the offending thread has the following callstack. I'm pasting below only the top few lines of the same for brevity:

    00000000`0023b188 00000000`78d6cf8b : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : ntdll!ZwWaitForMultipleObjects+0xa

    00000000`0023b190 00000642`7f668004 : 00000000`00000000 00000000`00000001 00000000`00000000 00000000`02a68420 : kernel32!ReleaseSemaphore+0x6b

    00000000`0023b2b0 00000642`b744efd2 : 00000000`00000001 00000642`b744b22b 00000000`00003a98 00000000`00000000 : mscorwks!IEE+0x7928

    00000000`0023b360 00000642`b744f1da : 00000000`02a68250 00000000`0023b488 00000000`00000000 00000000`00000000 : System_Data_ni+0xeefd2

    00000000`0023b450 00000642`b74b6b3e : 00000000`02a68250 00000000`02f06510 00000000`0023b500 00000000`00000000 : System_Data_ni+0xef1da

    00000000`0023b510 00000642`b7450132 : 00000000`0023b5b0 00000642`b77f6760 00000000`02f06558 00000642`7f664bc9 : System_Data_ni+0x156b3e

    00000000`0023b550 00000642`b7418cd0 : 00000000`02a4b7e8 00000000`02f06510 00000000`02a42a58 00000642`b741a463 : System_Data_ni+0xf0132

    00000000`0023b5b0 00000642`8017cda6 : 00000000`02f06510 00000000`00000000 00000000`0023b8d8 00000000`0023b750 : System_Data_ni+0xb8cd0

    00000000`0023b620 00000642`7f6688d2 : 00000000`02a72e40 00000000`02c9d988 00000000`00000000 00000000`00000000 : 0x642`8017cda6

    00000000`0023b740 00000642`7f5056b5 : 00000000`4374f680 00000000`00000000 00000000`00000000 00000000`00000000 : mscorwks!IEE+0x81f6

    00000000`0023b790 00000642`7f4f6636 : 00000000`0023b8c8 00000000`00000002 00000000`00001200 00000000`0023bad0 : mscorwks!CreateAssemblyNameObject+0x83d41

    00000000`0023b830 00000642`7f5a7560 : 00000642`7881aaf8 00000000`02c9d988 00000642`7881aaf8 00000642`7881aaf8 : mscorwks!CreateAssemblyNameObject+0x74cc2

    00000000`0023ba80 00000642`7f606e53 : 00000000`00000000 00000642`7f3d2a20 00000000`0023be18 00000000`0026fe50 : mscorwks!InitializeFusion+0x98b8

    00000000`0023bdd0 00000642`7809fc8d : 00000000`0023c030 00000000`02a72e40 00000000`02f063c8 00000000`0023bfe0 : mscorwks!MetaDataGetDispenser+0xa08f

    And I'm on a 64-bit version of Windows 2003, if that can be useful.

    Thanks again!

    Skandy.



  • Angelms

    Hi mark:

    Thanks for your reply. But that doesnt explain to me the reason for the hang. The application hangs indefinitely, and I see the same callstack as the one that I had posted earlier. Looks like the semaphore is not getting released for some reason. Performance considerations apart, is there anything with the DataGridView that would make it just difficult to yield

    Skandy



  • Roachy

    Thanks for posting the code. Much of the time will be spent in the reflection call(GetMethod) and in the call to the datasource (GetSourceView ). I suggest coming up with a caching mechanism so you don't do these calls every time the cell is formatted (a cell is formatted every time the cell is drawn or measured, so there are a lot of calls)

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"


  • Devon Kyle

    Can you post the code in the CellFormatting event It is faster to create a DataGridViewCellStyle instance when your app starts and then in the cell formatting event set the e.Style property to your cellStyle instance. This is the preferred way. You want to avoid creating a lot of font objects for each cell.

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"


  • Quantum_x

    Sorry - I didn't understand that is was an indefinite hang. I'm not aware of any code in the DataGridView that would cause this based upon the code you are executing. The DataGridView doens't hold locks on semaphores, but net (ADO) code might. I'm not aware if reflection uses semaphores or not.

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"


  • DataGridView Performance issues with CellFormatting