Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 WinForm
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 | :confused:
Posted 7-Sep-10 2:15am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you could use the cell formatting event because this event is fired only for cells that are visible
 
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
 
and put your color formatting code in the event handler
 
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
  Permalink  
Comments
Dalek Dave at 7-Sep-10 11:26am
   
Seems a good call.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Yes its possible without iteration
        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;
            }
        }
  Permalink  
Comments
T.Saravanann at 17-May-11 12:03pm
   
Very delayed...But surely useful in future... and others...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
T.Saravanann at 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 at 7-Sep-10 7:53am
   
"it will also take more time i think..."
I think not.
ambarishtv at 17-May-11 10:25am
   
hi see my solution
daveauld at 7-Sep-10 8:03am
   
""it will also take more time i think..." - you will only know if you try! never assume anything :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use the following code:
 
dataGridView1.DefaultCellStyle.BackColor = Color.RED;
 
Change the DefaultCellStyle to whatever you would like. This includes formatting, etc.
  Permalink  
Comments
T.Saravanann at 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 at 17-May-11 10:23am
   
see my solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 17 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100