Welcome to MSDN Blogs Sign in | Join | Help

jfo's coding

adventures in windows forms and wpf
DataGridView FAQ now posted

As promised: here's the link to the DataGridView FAQThere's also a set of demo applications.

There are whole bunch more FAQs up on Windows Forms.NET with quite a bit of good info in them. 

<shameless ask for plug>
Please check the FAQs out if you're thinking of using Windows Forms 2.0 -if it's useful, link to it so others can find them too!
</shameless ask for plug>

Thanks! =)

Posted: Wednesday, November 16, 2005 10:11 PM by jfoscoding
Filed under:

Comments

Girish said:

Hi jfo,
I downloaded the samples and the FAQ document. This grid is really cool. Thank you very much.

I also have some questions for you.

First, does the DataGridView allow ordering by multiple columns? For example, if I want to order columns first by Name, then by City, then by Locality etc, can I do that with DataGridView?

Second, does it allow grouping of columns? I've seen this in the Infragistics grid, wherein you drag a column to an area above it and the view changes to a list collapsible rows, and each row has the grouped column name and the group item as its caption. Is this possible?

Third, I saw some comments at this link that say the DataGridView is very slow. Could you please share your thoughts on this? Here is the link:
http://weblogs.asp.net/fbouma/archive/2005/10/29/428848.aspx

We are in the process of evaluating different options for our project, and your inputs would help us a lot.

Thanks and Regards,
Girish
# November 17, 2005 2:35 AM

jfoscoding said:

Good questions - lemme find out from the DGV people.
# November 17, 2005 3:13 AM

jfoscoding said:

Regis sent back a good chunk of information - I've posted his response here:

http://blogs.msdn.com/jfoscoding/articles/494012.aspx
# November 17, 2005 2:04 PM

Girish said:

Thanks very much, Jessica. This helped a lot. I will let you know if I need any other information.

Girish
# November 18, 2005 2:07 AM

Adam said:

This is a great FAQ. I'm curious, why would they make the Alternating Style override the DataGridViewColumn.DefaultCellStyle? It seems like that should be an underlying style more like DataGridView.DefaultCellStyle.
# November 29, 2005 7:18 PM

Adam said:

Jessica I have one more question for you. What do you think about SuspendLayout and ResumeLayout? I notice your FAQ doesn't mention those two methods. Will they help performance? I'm trying to optimize my DGV's as much as possible, but the ones with 500+ rows and approx 10 to 20 columns are very slow. Unfortunatly the ones with many rows are also the ones that I have to perform custom styles on cells based on DB values (ex: disable certain cells, so i set the style backcolor and forecolor to lightgray/darkgray). I can practically see each row get generated then after each row is added, i format the styles and you can see each style get applied. I'm trying to speed this up as much as possible and your FAQ is definitely a big help.
# November 29, 2005 7:35 PM

jfoscoding said:

Hmm. The first question I can answer, the second I cant without more information.

First question:
Just sort of the way it's designed. DefaultCellStyle is the one that comes into play when no one has voiced an opinion about what the colors, fonts etc should be. When you set AlternatingRowsCellStyle, you've voiced an opinion about (perhaps) the back color of the row. This needs to override the back color set in the DefaultCellStyle.

If you want to trump the AlternatingCellStyle, you'll need to use the DefaultCellStyle on the Row specifically, or a cell individually.

For best performance you should use the style that is the most general.

Regarding question #2 -
DataGridView.SuspendLayout and DataGridView.ResumeLayout prevent the OnLayout function from being called. I believe this may only help you if you've set a row/column to be autosized. You could override your DataGridView's OnLayout method and use the Stopwatch class (call stopwatch.Start(), base.OnLayout, stopwatch.Stop()) - however in this case, I might start with a bit of divide and conquer.

Make a backup of your code, then start commenting out pieces and parts until you can isolate what's making it slow. Once you've isolated the main thing that makes it slow, uncomment unrelated stuff until you've isolated whats gone wrong.

If you still are unable to figure it out, you can use the MSDN forums[1] or push the contact link on my blog to further help isolate what you're seeing.

[1] http://forums.microsoft.com
# November 29, 2005 9:19 PM

Adam said:

Thanks Jessica. Your response was very helpful. I just finished implementing the CellFormatting event (which I learned about from your FAQ) for doing individual styles. Before I was just looping through the rows and setting the styles, after the datasource got set. It has improved the speed significantly on my grids with lots of rows. It has slowed down my smaller grids slightly, but it was definitely worth it.

Do you happen to know if there's anyway to cache the styles or something after they get initially set by the CellFormatting event, so that they don't have to get redrawn each time? If not, it's not biggy. I appreciate all the help.

Thanks again.
-Adam
# December 1, 2005 1:10 PM

jfoscoding said:

Lets continue this thread on the forums:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=154156&SiteID=1

This way I can have the DGV people directly answer your questions.
# December 1, 2005 4:55 PM

datagrid | hilpers said:

# January 17, 2009 3:45 PM
New Comments to this post are disabled
Page view tracker