473,499 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView performance

Hi there,

Has anyone had any serious performance problems with this class. A couple of
hundred rows of data with 4 columns (everything loaded on-the-fly with no DB
involved, all simple strings, 90% under 10 characters, the rest as high as
500-600 characters, wrapping turned on) literally takes almost 3 minutes to
load on my 2.4 MHz machine (1 GB RAM with plenty to spare). Wrapping may be
the bottleneck (performance improves when I turn it off) but other than
using virtual mode, can anyone suggest a way to speed things up (or a
reliable 3rd-party control which is really something I wanted to avoid).
Thanks.
Nov 24 '06 #1
9 17921
Looks pretty quick to me...
Try this: it invents 5000 rows of data using 4 string properties, random
length up to 600 chars; loads almost instantly on similar spec PC, even
including property notification handlers; have you got some example code?

using System;
using System.Windows.Forms;
using System.Collections.Generic;
class MyData
{
private string fieldA, fieldB, fieldC, fieldD;
private void OnEvent(EventHandler handler)
{
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler PropertyAChanged, PropertyBChanged,
PropertyCChanged, PropertyDChanged;
public string PropertyA
{
get { return fieldA; }
set
{
if (fieldA != value)
{
fieldA = value;
OnEvent(PropertyAChanged);
}
}
}
public string PropertyB
{
get { return fieldB; }
set
{
if (fieldB != value)
{
fieldB = value;
OnEvent(PropertyBChanged);
}
}
}
public string PropertyC
{
get { return fieldC; }
set
{
if (fieldC != value)
{
fieldC = value;
OnEvent(PropertyCChanged);
}
}
}
public string PropertyD
{
get { return fieldD; }
set
{
if (fieldD != value)
{
fieldD = value;
OnEvent(PropertyDChanged);
}
}
}
}
static class Program
{
static readonly Random rand = new Random(123456);
static string RandomString() {
int length = rand.Next(600);
char[] chars = new char[length];
for (int i = 0; i < length; i++)
{
chars[i] = (char) rand.Next(32, 122);
}
return new string(chars);
}
static void Main()
{
const int COUNT = 5000;
List<MyDatadata = new List<MyData>(COUNT);
for (int i = 0; i < COUNT; i++)
{
MyData item = new MyData();
item.PropertyA = RandomString();
item.PropertyB = RandomString();
item.PropertyC = RandomString();
item.PropertyD = RandomString();
data.Add(item);
}
using(Form f = new Form())
using (DataGridView dgv = new DataGridView())
{
dgv.Dock = DockStyle.Fill;
dgv.DataSource = data;
f.Controls.Add(dgv);
Application.Run(f);
}
}
}

Marc
Nov 24 '06 #2
(note still performant if I add dgv.DefaultCellStyle.WrapMode =
DataGridViewTriState.True;)

Marc
Nov 24 '06 #3
(note still performant if I add dgv.DefaultCellStyle.WrapMode =
DataGridViewTriState.True;)

Marc
Thanks very much (appreciated). I'll try your sample and let you know ...
Nov 24 '06 #4
As a guess, I would imagine that the problem is that you are adding rows
while it is UI bound, so each insert needs to think about a redraw. Some
controls have a BeginUpdate / EndUpdate pair to help with this, but you
could try disabling layout - it *may*help:

dgv.SuspendLayout();
try {
// your code
} finally {
dgv.ResumeLayout();
}

Alternatively, if you are using a BindingSource, that has a SuspendBinding /
ResumeBinding, or as a last resort you could remove the data-source (from
the grid) completely while editing.

Again, I could probably help more with a code snippet *that is runnable and
demonstrates the problem*.
(Jon puts it very well: http://www.yoda.arachsys.com/csharp/complete.html)

Marc
Nov 24 '06 #5

"John Brown" <no_spam@_nospam.comwrote in message
news:ON****************@TK2MSFTNGP06.phx.gbl...
>(note still performant if I add dgv.DefaultCellStyle.WrapMode =
DataGridViewTriState.True;)

Marc

Thanks very much (appreciated). I'll try your sample and let you know ...
Ok, it does load almost instantly now. Painting is still painfully
(unacceptably) slow however (navigating as well). I did find the original
problem though. Try setting "AutoSizeColumnsMode" to
"DataGridViewAutoSizeColumnsMode.AllCells" Now it takes forever to load.
Have you used the older "DataGrid" class. I understand it's much faster but
without all the bells and whistles. Do you have any opinion on it. Thanks.
Nov 24 '06 #6
"Marc Gravell" <ma**********@gmail.comwrote in message
news:uO****************@TK2MSFTNGP03.phx.gbl...
As a guess, I would imagine that the problem is that you are adding rows
while it is UI bound, so each insert needs to think about a redraw. Some
controls have a BeginUpdate / EndUpdate pair to help with this, but you
could try disabling layout - it *may*help:

dgv.SuspendLayout();
try {
// your code
} finally {
dgv.ResumeLayout();
}

Alternatively, if you are using a BindingSource, that has a SuspendBinding
/ ResumeBinding, or as a last resort you could remove the data-source
(from the grid) completely while editing.

Again, I could probably help more with a code snippet *that is runnable
and demonstrates the problem*.
(Jon puts it very well: http://www.yoda.arachsys.com/csharp/complete.html)
Already investigated "SuspendLayout()" with no success. It's starting to
look like there's little I can do other than relying on virtual mode. I also
found this (where MSFT provides some possible remedies but basicallly
acknowledges the "lower performance" and perhaps a "fast painting mod" for a
future release).

http://connect.microsoft.com/VisualS...dbackID=117093
Nov 24 '06 #7
Interestingly, it appears to take about 3-4 times as long if it does this in
the inital load. I'm guessing due to resizing fonts etc requiring a re-draw.

Try the following, which loads the data when you first drag the mouse over
the grid - it loads in about 5 seconds on my poor-mans laptop (1.8, 512) -
so should be better for you. Obviously you might want to put this in a small
timer or something rather than rely on user intervention (I'm guessing that
using Load would still cause the redraws; haven't tested though)

Main difference is where the data gets initialised (although the important
bit really is when the bindings get set against the dgv, or in this case,
reset):

static void Main()
{
const int COUNT = 5000;
bool loaded = false;
BindingList<MyDatadata = new BindingList<MyData>();
using(Form f = new Form())
using (DataGridView dgv = new DataGridView())
{
dgv.MouseEnter += delegate
{
if (loaded) return;
data.RaiseListChangedEvents = false;
try
{
data.Clear();
for (int i = 0; i < COUNT; i++)
{
MyData item = new MyData();
item.PropertyA = RandomString();
item.PropertyB = RandomString();
item.PropertyC = RandomString();
item.PropertyD = RandomString();
data.Add(item);
}
}
finally
{
data.RaiseListChangedEvents = true;
data.ResetBindings();
loaded = true;
}
};
dgv.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
dgv.DataSource = data;
dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgv.Dock = DockStyle.Fill;
f.Controls.Add(dgv);
Application.Run(f);
}
}
Nov 24 '06 #8
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
Interestingly, it appears to take about 3-4 times as long if it does this
in the inital load. I'm guessing due to resizing fonts etc requiring a
re-draw.

Try the following, which loads the data when you first drag the mouse over
the grid - it loads in about 5 seconds on my poor-mans laptop (1.8, 512) -
so should be better for you. Obviously you might want to put this in a
small timer or something rather than rely on user intervention (I'm
guessing that using Load would still cause the redraws; haven't tested
though)

Main difference is where the data gets initialised (although the important
bit really is when the bindings get set against the dgv, or in this case,
reset):
It still took several minutes to render itself but that's better than
before. Something's obviously wrong however given that it returns in 5
seconds on your laptop. What I'll have to do is look at your code in greater
detail (BindingList, etc.) and combined with other possible remedies at
MSFT's site (cited in my other post), maybe I can handle painting/drawing on
a page-by-page basis (without turning to virtual mode). With any luck I can
improve things after some experimentation. If worse comes to worse however,
do you have any experience with the "DataGrid" or some other 3rd-party
control. I don't need anything flashy but functional control is important
(the grid's look should be clean and modern - not outdated - but my needs
are mostly utilitarian). Thanks for all your help (truly appreciated)
Nov 24 '06 #9
You're welcome... and if you find what is causing the poor performance,
please let us know.
Alternatively, if you can reproduce it in a standalone example (even if
you can't explain it), then please feel free to post it - I know I
would be interested in having a look, and I'm sure it would pique a few
others too...

Marc

Nov 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2158
by: orekinbck | last post by:
Hi There In a C# 2.0 application I have a DataGridView that contains thousands of rows. There is some fairly complex business logic that determines: - Row forecolor - Row backcolor - Cell...
1
1955
by: FX | last post by:
Hi all, I'm using a CLR 2 component DataGridView and I'm encountered Performance issue. Here is the snippet : string valueStr = value.ToString(); DataGridViewRowCollection rows =...
7
9662
by: steve | last post by:
Hi All I urgently need help on setting datagridview cell borders at runtime I found some code on the web from Programming Smart Client Data Applications with .NET 2.0 by Brian Noyes See...
5
2131
by: sp | last post by:
Hello I have a problem with the refresh performance in datagrid – when datagrid is being shown it is so slow that I can see one by one cells is drawn -datagrid contains about 35x40 of...
1
7760
by: Karl | last post by:
Hi all... This is a good one. You'll like this... I am working on a course management tool that allows certain Courses to be cross referenced with Job Roles and, when they are, whether the...
2
2894
by: M R | last post by:
I need to build and display a huge data grid (roughly 5000x5000). (I know it is difficult to view, but i need to do it anyway) the immediate problem i am facing is that it takes a VERY long time...
3
5804
by: azraiyl | last post by:
Hello, When I create a DataTable and add 10000 rows my computer needs about 20ms. To delete 10000 rows it needs about 15ms. When I connect the DataTable to a DataGridView it needs 3100ms to...
3
2151
by: Tony K | last post by:
VB 2005 - Windows Vista I have a form that seems to lock up when the number of rows exceed the height of the datagridview. The following code executes when data is received through the serial...
7
1799
by: Bill Schanks | last post by:
VB.NET 2005 SQL Server 2000 I have a datagridview that users will update. When there are only a few records the update is fine. However when they update 100-200 of the rows the udpate can take...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.