views:

381

answers:

3

Hi there!

In my winforms app, i have a datagridview that takes about 0.8 seconds to be populated with +/- 5000rows - if all columns are textbox columns.

One of the columns is an integer column, so I decided to change that column to an ImageColumn and in the Cell_formatting event of the grid, I use the following code to determine the appropriate image to display:

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (dgv.Columns[e.ColumnIndex] is DataGridViewImageColumn && e.ColumnIndex == 1) {
        int cellVal = (int)e.Value;
        switch (cellVal) {
            case 1:
                e.Value = Properties.Resources.Pending;
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "Item pending attention";
                break;
            case 2:
                e.Value = Properties.Resources.Tick
                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "File is available";
                break;
            default:
                break;
        }
    }
}

It works. However, the datagridview now takes almost 5 seconds to populate itself!! and sorting columns just becomes too time consuming. 5 seconds now may not seem like a lot, but the rows will grow to about 30,000 in about 2months time!!

Is there a more efficient way to handle this scenario?

Thank u!

+1  A: 

This may not help at all, but you could try doing the e.ColumnIndex == 1 comparison first. Since && is a short-circuiting operator, you won't be evaluating dgv.Columns[e.ColumnIndex] is DataGridViewImageColumn for every column.

Edit: I would really use something like Visual Studio Profiler or some other performance tool to find bottlenecks.

Andy West
Profiler only available in TEam Suite...im sitting with Professional. But i remember seeing somewhere that there is a command-line version of it available for other editions.your post makes sense though. I think I was merely being extra cautious without considering the extra overhead. I don't see how much of a difference it will make but I will certainly try it! thanks!
Shalan
+1  A: 

Have you checked that the bottleneck isn't in the reading and (possible) resizing of the images?

If you're displaying the images at 32x32 pixels (say) but they are stored as 128x128 pixels (say) then there will be a resize every time the image is displayed.

ChrisF
Yikes! nope, they're all stored and used as 10x10 pixel images. I forgot to mention before that Im doing my databinding using the BackgroundWorker, but it shouldnt make a difference, because what Im experiencing is affecting the Ui thread only
Shalan
Is there perhaps a different/better datagridview event to handle instead?
Shalan
BTW - each image is 579 bytes in filesize
Shalan
Thanks Chris! Oh, one more thing...my data query returns a DataTable, and my Grid is bound to its DefaultView.. Would that make a difference? (Iv tried asking this same question here without concrete resolve: http://stackoverflow.com/questions/1761597/performance-regarding-return-type-for-linq-query-compatible-with-automatic-sortin)
Shalan
+1  A: 

If performance is a concern, you may consider using the DataGridView in virtual mode.

Sorin Comanescu
Hey again sorin! thanks for the reply. Will virtual mode aid in Data Performance or UI Performance of the Grid itself? If the former, then it wont make a diff to me. My bottleneck is the actual grid formatting event. I dont know if virtual over bound mode will make a difference
Shalan
Hey, Shalan. I would say both (although for data performance things need a more complex approach, like detailed here: http://msdn.microsoft.com/en-us/library/ms171624.aspx). I did run some simple(istic?) tests (preloaded data then added to the datagrid in both "classic" mode and virtual mode) and the UI boost is huge.
Sorin Comanescu
"I dont know if virtual over bound mode will make a difference" - give it a try, what could you lose?
Sorin Comanescu
Yes, u r right - what do I have to lose? :)<br>But for my purposes, my grid is completely read only - no adding, editing, deleting on the grid itself. What events/methods should I be addressing at minimum if I go the Virtual Mode route?
Shalan
At a minimum, for a quick test, you could start with the DataGrid in virtual, unbound mode and handle the CellValueNeeded event. If you're satified with the performance, the two links I gave above should allow you to go in much more depth.
Sorin Comanescu
Hey Sorin! after a bit of trial an error, I did manage to get it working...and much to my surprise, it did make a bit of a difference. Its obviously not instantaneous, but I'll try playing around with it to tweak it up. Thanks again!
Shalan