Hi. I am creating an asp.net web site. Using GridView the website creates a table. Now my question is I want to color the text "NO" red within the table cells. Waiting for your reply.Please keep in mind my code is written in C# so any C# examples would be greatly welcomed...

Recommended Answers

All 7 Replies

Examining the GridView's Aesthetic Properties
Like the DataGrid in ASP.NET 1.x, the GridView contains a wealth of properties that can be set to gussy up the appearance of the GridView. Properties such as BackColor, Font, and ForeColor allow you to dictate the aesthetics for the entire GridView. You can also specify aesthetic settings specific to particular parts of the GridView through the HeaderStyle, RowStyle, AlternatingRowStyle, FooterStyle, and other properties. Additionally, styles can be set on a column-by-column basis, as well.

If you are an artistic person you can likely put together proper colors and styles that will provide the end user with an eye-pleasing GridView. If you are like me, though, the good news is that the GridView can be Auto Formatted to a variety of well put together styles. To take advantage of the Auto Formatting, simply click the Auto Format link in the GridView's Smart Tag, which will display the Auto Format dialog box:-/

Thanks. But I only want to color certain words with certain colors. I don't think Auto Format resolves my problem since OK or NO can be anywhere within the table cells. Can this be done with a C#? if so how? Thank you in advance

Thank your for the link. I think I can cook up something with the information you gave me.

Use RowDataBound event. Find the control using FindControl. Check the value of the control and depending on the value set the FontColor property.

hi,

u can use below for adding color to gridview

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i,j;
string c;


for (i = 0; i <= GridView1.Rows.Count - 1; i++)
{
for (j = 0; j <= GridView1.Rows.Cells.Count-1; j++)
{
c = GridView1.Rows.Cells[j].Text;
if (c == "NO")
{
//GridView1.Rows.Cells[j].BackColor = System.Drawing.Color.Blue;
GridView1.Rows.Cells[j].ForeColor  = System.Drawing.Color.Blue;
}
else if (c == "OK")
{
//GridView1.Rows.Cells[j].BackColor = System.Drawing.Color.Red;
GridView1.Rows.Cells[j].ForeColor  = System.Drawing.Color.Red;
}
}
int k;
k = GridView1.Columns.Count;
}
}

shailu

commented: this code saved my life.It looks like a simple yet effective to paint within a gridview.... +4
commented: Good solution. Use code tags next time.:) +3

Thank you so much .... You got my star!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.