Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi All,

I am using Datagridview in my windows application form.I am applying different colors for different rows and columns based on certain values in a particular cell.

Here is my code,

for(int x=0;x<dgv.RowCount;x++)
   {
    if(dgv["Dept",x].Value.ToString().Contains("MANF"))
    {
      if(dgv["Name",x].Value.Tostring().Startswith("A"))
         dgv["Name",x].Style.BackColor=Color.Blue;  
    else if(dgv["Name",x].Value.Tostring().Startswith("B"))
         dgv["Name",x].Style.BackColor=Color.Red;
     else  if(dgv[&quot;Name&quot;,x].Value.Tostring().Startswith(&quot;C&quot;))
         dgv[&quot;Name&quot;,x].Style.BackColor=Color.Yellow;
        .
        .
        .
        .
     }

   } 


No.of records in datagridview is more than 10000.So Coloring takes more than a minute because of the loop.

My question is any possibilities (or) events are there to achieve my output without using any loop condition.

Advance Thanks,
:confused:
Posted

you could use the cell formatting event because this event is fired only for cells that are visible

C#
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);


and put your color formatting code in the event handler

C#
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //formatting code in here
        }


I hope you are binding to the datagridview ... (instead of manually creating each row).

Hope that helped!
good luck
 
Share this answer
 
Comments
Dalek Dave 7-Sep-10 11:26am    
Seems a good call.
Yes its possible without iteration
C#
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{

    DataGridView grd = sender as DataGridView;
    if (grd.Rows[e.RowIndex].Cells[1].Value.ToString() == "STOPED")
    {
        grd.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.HotPink;
    }
    if (grd.Rows[e.RowIndex].Cells[1].Value.ToString() == "ACTIVE")
    {
        grd.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
    }
}
 
Share this answer
 
Comments
T.Saravanann 17-May-11 12:03pm    
Very delayed...But surely useful in future... and others...
One option is to only colourise on a scroll event.

You obviously cannot see all 10000 rows at once, and will need to scroll up and down to see them.

Colourise the rows as you scroll them.

Look at my tip on sizing columns and apply the same principles to colourisation.

http://www.codeproject.com/Tips/88705/DataGridView-Column-Width-Grow-Only-Sizing.aspx[^]
 
Share this answer
 
Comments
T.Saravanann 7-Sep-10 7:38am    
Actually i am not show all the records in datagridview.I have the records in datatable after filter that records and then show in datagridview.If i scroll the datagrid how to get the particular cell value at the time to check the condition..it will also take more time i think...
Toli Cuturicu 7-Sep-10 7:53am    
"it will also take more time i think..."
I think not.
ambarishtv 17-May-11 10:25am    
hi see my solution
DaveAuld 7-Sep-10 8:03am    
""it will also take more time i think..." - you will only know if you try! never assume anything :)
Use the following code:

dataGridView1.DefaultCellStyle.BackColor = Color.RED;


Change the DefaultCellStyle to whatever you would like. This includes formatting, etc.
 
Share this answer
 
Comments
T.Saravanann 13-Sep-10 9:17am    
Thanks,But i am apply a color row by row and also column wise.How to achieve this one without loop because if using loop means it takes time.
ambarishtv 17-May-11 10:23am    
see my solution
Do not colour the cells in a loop or in a scroll event. The only recommended way of doing this is by subscribing to the CellFormatting event and use the DataGridViewCellFormattingEventArgs to determine the colour to use and perform the colouring.

Additionally, for a dataset with 10000 rows I would strongly recommend setting VirtualMode to true. Operating a DGV in virtual mode is much easier than it looks. In the case of a read-only grid you only need to tell it how many rows there are and handle one event. For a read/write grid it's just a couple more events. Your application will be much more responsive and your users will thank you.
 
Share this answer
 

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