Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET NoEffort
i have an asp: datagrid which contain loat of template column.i want to change the back grond color in the grid row/cells based on the colum value lets say "Type" .datagrid is binded using code .
Posted 25-Apr-13 3:25am
rajin kp1.9K
Edited 25-Apr-13 3:57am
CHill6067K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is an article about that in CodeProject (and about a quarter of a million hits in Google)
Changing the color of a row in a GridView in ASP.NET[^]

 
Only about 200k hits in google
http://stackoverflow.com/questions/11135353/datagrid-row-color-change-based-on-data[^]
  Permalink  
v2
Comments
rajin kp at 30-Apr-13 1:32am
   
bro , do you know data grid and grid view are different :)
CHill60 at 30-Apr-13 4:12am
   
But in both cases you use Row.Backcolor.
The point that it's incredibly easy to find this solution in google is the same though :-) I've updated my solution with a different link
Tiwari Avinash at 30-Apr-13 5:41am
   
nice link!! I think rajin kp got some help from it surely.. :)
rajin kp at 1-May-13 5:42am
   
i solve this as
protected void OTSDGView_ItemDataBound(object sender, DataGridItemEventArgs e)
{
try
{
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "AppointmentTypeID")) == "1")
{
e.Item.CssClass = "tr_Analysis";
}
else if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "AppointmentTypeID")) == "2")
{
e.Item.CssClass = "tr_Dietitian";
}
else if (Convert.ToString(DataBinder.Eval(e.Item.DataItem, "AppointmentTypeID")) == "3")
{
e.Item.CssClass = "tr_Doctor";
}
//}
}
catch (Exception ex)
{
//ErrorLabel.Text = ex.Message;
}

}
tks all
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
You can use jQuery to change background color of tr elements or you can modify this code jQuery selectors.
 
 $(document).ready(function () {
        $('tr:even').addClass('alt-row-class');
        $('tr:odd').addClass('normal-row-class')
    });
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I'm taking into account that you are using DataGrid and not the GridView of .net
 
you can use OnItemDataBound event of the datagrid.
 
use something like this in event
 
if(ControlValue < 3)
{
    TableCell Cell = e.Row.Cells[0];
      Cell.BackColor = Color.Black;
}
 

or use the "e" event argument to find the cell instead of table cell
  Permalink  
Comments
rajin kp at 1-May-13 6:48am
   
controlvalue is avilable after databound isnt it ?

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2013
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