Click here to Skip to main content
11,721,053 members (80,474 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 2:25am
rajin kp1.9K
Edited 25-Apr-13 2:57am
CHill60105.4K
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 Sergey Alexandrovich Kryukov 500
1 OriginalGriff 334
2 Mika Wendelius 160
3 F-ES Sitecore 140
4 Maciej Los 80
0 Sergey Alexandrovich Kryukov 2,199
1 OriginalGriff 959
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2015
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