Change background color of GridView's Rows |
Visite: 63984 |
martedì 18 aprile 2006 |
Framework .NET 2.0
On the Web is possible to find many examples on how to change the background
color of row based on the data using GridView control, I wanted to post my personal
and simple example on "how-to-do" it.
I suppose to have one database's field named IsNew and when it's value is set to
true,
then the GridView rows has to have different background color.
I use the
RowDataBound
event:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
...
</asp:GridView>
The C# code for the event will be:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// searching through the rows
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isnew = (bool)DataBinder.Eval(e.Row.DataItem, "IsNew");
if ( isnew ) e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row
}
}
|
It's a awesome article, but i have some problems in my implementation. When I try run my web aplication, it show me some erros:
Error 1 The name 'Color' does not exist in the current context C:\Documents and Settings\alyson\My Documents\Visual Studio 2005\TWeb\WebSite2\Default.aspx.cs 112 42 C:\...\WebSite2\
Can you help me? Thanks a lot!
|
| Scritto da Alyson - giovedì 8 marzo 2007 alle ore 13.24 |
|
Try by adding System.Drawing at the top of the .cs file.
|
| Scritto da Ruben - martedì 20 marzo 2007 alle ore 18.53 |
|
That did it for me! Works great and is clean.
|
| Scritto da Charles Evans - mercoledì 4 aprile 2007 alle ore 0.26 |
|
Thank you very much, as much concentration as there is regarding databinding to the gridView control, there is not enough elaboration on tailoring the control once it is bound. Again thank you.
|
| Scritto da Nihlist - martedì 29 maggio 2007 alle ore 23.13 |
|
Thank you very much, that solved my problem!
|
| Scritto da Bennie - mercoledì 14 novembre 2007 alle ore 10.02 |
|
Hi, Ive tried your code and it works perfectly, im a bit of a newbie though and Im experiencing a problem once I select another item the color formatting disappears. Is this common or did I screw up a setting somewhere?
Thanks
J
|
| Scritto da JoE - domenica 29 giugno 2008 alle ore 19.02 |
|
Hi,
I use code below to change the color of the row according to the data from database....
if (e.Row.RowType == DataControlRowType.DataRow)
{
// determine the value of the UnitsInStock field
int TicStatusID =
Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
"strShift"));
if (TicStatusID.ToString() == "White")
// color the background of the row yellow
e.Row.BackColor = System.Drawing.Color.White;
} -----------------------------------------------------------------------------------------------------------
but it show error like this...---------------------------------------------------------------------
Error 11 'System.EventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\SS\My Documents\Visual Studio 2008\WebSites\Helpdesk\Dashboard2.aspx.cs 23 15 C:\...\Helpdesk\
---------------------------------------------------------------------------can you help me? thank you very much...
|
| Scritto da Meshack - martedì 15 luglio 2008 alle ore 4.14 |
|
Yes i can help you...
Just read the error message you have posted and analyze - It says that "System.EventArgs" has noch "Row" property. In other words: you are using the not existing "Row"-property of an object of type "System.EventArgs".
I highly suggest you forgot to cast the "EventArgs" - Parameter to the type "GridViewRowEventArgs".
So just change the parameter type of your function from "EventArgs" to "GridViewRowEventArgs" and you will do right!
|
| Scritto da fireface - giovedì 7 agosto 2008 alle ore 15.07 |
|
Thanks, worked wonders.
I used this styling to get the "inactive" rows grayed out.
if (!active)
{
e.Row.Style.Add("background-color", "#F6F6F6");
e.Row.Style.Add("color", "#999999");
}
|
| Scritto da Sako - lunedì 4 maggio 2009 alle ore 12.45 |
|
thanks very much it help me .
|
| Scritto da zatar - mercoledì 14 ottobre 2009 alle ore 12.46 |
|
This entry has helped me on this day. Thank you.
|
| Scritto da LWatts - mercoledì 17 febbraio 2010 alle ore 6.07 |
|
For a applied css gridview its not working.
|
| Scritto da Kiran - mercoledì 17 febbraio 2010 alle ore 10.19 |
Scrivi nuovo commento