Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 25-Apr-13 2:57am
v2

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

C#
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
 
Share this answer
 
Comments
rajin kp 1-May-13 6:48am    
controlvalue is avilable after databound isnt it ?
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
 
Share this answer
 
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[^]
 
Share this answer
 
v2
Comments
rajin kp 30-Apr-13 1:32am    
bro , do you know data grid and grid view are different :)
CHill60 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 30-Apr-13 5:41am    
nice link!! I think rajin kp got some help from it surely.. :)
rajin kp 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

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