Click here to Skip to main content
15,742,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I have a problem and a question, dg is datagridview that I have which I populate with created binding source

this is the code

C#
daActivity.SelectCommand = new SqlCommand("SELECT * from DataActivity", cs);
dsActivity.Clear();
daActivity.Fill(dsActivity);

ActivityBS.DataSource = dsActivity.Tables[0];

dg.DataSource = ActivityBS;

and on cell formatting event I have this code

C#
for (int i = 0; i < dg.RowCount; i++)
            {
                string status = dg.Rows[i].Cells[12].Value.ToString();
                if (status == "OPEN")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightCoral;
                }
                if (status == "INFO")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;
                }
                if (status == "CLOSED")
                {
                    dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGreen;
                }
            }

and this event slows down populating datagridview (dg)......why???? is there a way to overcome this, or maybe something in my code or ....well I need assistance and opinion
Posted
Updated 26-Aug-19 1:37am
v2
Comments
__TR__ 31-Aug-12 14:28pm    
Try Replacing the 3 if statements in your code with a switch statement.
shonezi 31-Aug-12 14:31pm    
how to do that??
__TR__ 31-Aug-12 14:32pm    
Refer:
http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.100).aspx
shonezi 31-Aug-12 14:38pm    
I am a little lost on this one
__TR__ 31-Aug-12 14:44pm    
Its like this:

string status = dg.Rows[i].Cells[12].Value.ToString();
switch (status)
{
case "OPEN":
dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightCoral;
break;
case "INFO":
dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightBlue;
break;
case "CLOSED":
dg.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGreen;
break;
default:

break;
}

It's difficult to be sure without running it, but the first thing you should notice is that CellFormating is called on a per-cell basis: since you are looping through all the rows every time it is called, you may find this to be a source of slowdown.

The first thing to do though, is to get some numbers - until you have a good idea of how much time is being spent where, you can't be sure that you are making any real improvement.

Look at the Stopwatch class[^] - it provides you with an accurate method of timing from point to point. Start with a timer around the DataSource setting, and then add one in the CellFormatting event. See what numbers you get, and you can start working out where things are slow.
 
Share this answer
 
Comments
shonezi 31-Aug-12 14:19pm    
I did what you suggested, I get around 300 miliseconds response at start, but when it finishes populating it shows 6. problem is also that when I scroll up and down the datagridview it is also slow
OriginalGriff 31-Aug-12 14:42pm    
Try printing the event stopwatch to a console - it will affect the timings, but it'll give you an idea of how often and how long each individual event is taking.

As to the scroll, that doesn't surprise me at all - remember that the CellFormating event will be called for each cell *as it is displayed* - so any loop which runs through every row is going to be executed a lot when you scroll!
shonezi 31-Aug-12 14:51pm    
well, you are totally right so I did this, I placed all of the code with stopwatch to Shown event, to raise it just once on form load and its 0 miliseconds, works fast. maybe this is ok, is it?
OriginalGriff 31-Aug-12 15:15pm    
If the data doesn't change then yes, it's probably fine - you know your system better than I do! :laugh:
shonezi 31-Aug-12 16:18pm    
thank you, and thank you TR and Wes Day :))
In my case the problem was caused by DataGridView property AutoSizeColumnsMode=AllCells. Probably after cell formatting all other cells in column and header cell had to be redrawn to fit theirs dimensions to the new cell dimensions. After I was changed property value to it default value "None", grid is painted immediatel
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900