Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a Gridview Control in my Web Application.I want to highlight some rows with red colour based upon the values from database.

Can you suggest some solution to this requirement.


Thanks in advance.
Posted
Updated 17-May-11 21:22pm
v3

See this discussion:
http://forums.asp.net/p/1478345/3443644.aspx[^].

—SA
 
Share this answer
 
Comments
Abhinav S 18-May-11 1:50am    
My 5 as well.
Sergey Alexandrovich Kryukov 18-May-11 2:35am    
Thank you, Abhinav,
--SA
[no name] 18-May-11 2:07am    
My 5 too.
Sergey Alexandrovich Kryukov 18-May-11 2:35am    
Thank you, Ramalinga.
--SA
Kaira_29 18-May-11 2:54am    
Thanks a lot
Capture the RowDataBound event and then place your logic here

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // searching through the rows
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
            if(MyDatabaseCondtion()){
                e.Row.ForeColor = System.Drawing.Color.Red;
            }
    }
}


The MyDatabaseCondition() method will check the database condition that you require.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-May-11 1:41am    
This is correct, my 5. I actually provided the link to the same exact solution, please see.
--SA
Abhinav S 18-May-11 1:50am    
Thank you.
[no name] 18-May-11 2:07am    
My 5 too.
Abhinav S 18-May-11 2:08am    
Thanks.
ambarishtv 18-May-11 3:26am    
my 5 :)
 
Share this answer
 
Hi,
Let your ID is in Column(1) of the GridView and you want to change the Backcolor of all rows having ID as "3"
why don't you try this.
protected void GridView1_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            if (gvr.Cells[1].Text == "3"){
            
                gvr.BackColor = System.Drawing.Color.Beige;
            }
        }
    }
or

you can change back color of gridview using rowdatabound event as follows
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = Color.Blue;
        }

Moreover, apart from rowdatabound event, you can also change backcolor of grid after binding it with data as follows
for (int i = 0; i < gv.Rows.Count; i++)
        {
            gv.Rows[i].BackColor = Color.Pink;
        }
 
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