Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I am using Grid view where data is coming like below
HTML
Rack 1   2   3   4   5   6
A1   20              40 
A2       60  45          20
B1   34          30  20  10
C1           23          40


Now my question how can I make blank cell green colour and filled cell blue colour

Thanks to All
Posted

I am assuming that you are using ASP.Net C#.
Create GridView1_RowDataBound event for your GridView.

C#
if (e.Row.RowType = DataControlRowType.DataRow) 
{     
  //Check your condition here     
  If(Condition True)    
  {         
     e.Row.BackColor = Drawing.Color.Red // This will make row back color red     
  } 
} 


For windows application:
C#
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{      
    if (e.Value != null)      
    {          
         if (e.Value.Equals(string.Empty))              
            e.CellStyle.BackColor = Color.Green;
         else
            e.CellStyle.BackColor = Color.Blue;
      
    }                
} 
 
Share this answer
 
v2
Comments
IndrajitDasgupat 6-Aug-12 3:29am    
I am using windows application under which event I should I put this code
Vani Kulkarni 6-Aug-12 4:26am    
Hi Indrajit,
I have updated the solution!
AshishChaudha 6-Aug-12 4:29am    
my 5!
Vani Kulkarni 6-Aug-12 4:59am    
Thanks Ashish. :)
Maciej Los 6-Aug-12 4:57am    
Good answer, my 5!
This is called Conditional Formatting. Try the code below hope it helps you move on.

You can enable the RowDataBound Event in the markup
ASP.NET
<asp:gridview id="gridview1" runat="server" onrowdatabound="RowDataBound" xmlns:asp="#unknown">

   </asp:gridview>

And put this in your Code-Behind file.

C#
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
   {
       if(e.Row.RowType == DataControlRowType.DataRow)
       {
           if(e.Row.RowIndex == 0)
               if(e.Row.Cells[0].Text == string.Empty)
                   e.Row.Cells[0].BackColor = Color.Green;
               else
                   e.Row.Cells[0].BackColor = Color.Blue;
       }
   }
 
Share this answer
 
v2
Comments
Vani Kulkarni 6-Aug-12 3:08am    
Ditto +5!
IndrajitDasgupat 6-Aug-12 3:18am    
But I am using windows application in this case what should I do
IndrajitDasgupat 6-Aug-12 5:04am    
Your new update is showing all same colour except first cell
But I need blue colour only in 20,40,60,45,35,...,23 and 40 and blank box green colour and A1,A2,B1,C1 should be default data grid view colour
Thanks for your support

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