| I have a Visual Studio Web application that I want to change individual datagrid cell colors depending on the data they hold. | |
| Is there a way to change the background colors of individual cells in a datagrid using C#? | |
Changing background color of individual datagrid cells
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Grzegorz7 pointsPosted Mar 31, 2006, 6:50 AM EST
try http://www.dotnet247.com/247reference/articles/1/8355.aspx
Hope it helps.
Regards and good luck,
Grek.
Adam Sta3 pointsPosted Mar 31, 2006, 8:17 AM EST
Its using C# Windows forms and not a C# web application... I couldnt seem to get the code working in a Web App...
Is it possible to use similar code to this in a web app or would it be totally different?
ray4 pointsPosted Apr 7, 2006, 9:45 AM EDT
need to create your own class that inherits from DataGridTextBoxColumn. override
the paint method and then you are set.
See:
http://www.codeproject.com/csharp/custom_datagridcolumnstyl.asp
For more help also look at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp
for a VB example
Hope that this helps
ray4 pointsPosted Apr 7, 2006, 10:02 AM EDT
the ItemDataBound event. At that point just add an attribute to the cell
Somthing like this will do:
private void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
if(row["FieldName"].ToString == "Your Value")
{
e.Item.Cells[iColNum].Attributes.Add("style","your style");
}
}
}