| 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#? | |
Loading
| 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#? | |
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
rayPosted Apr 7, 2006, 2:02 PM
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");
}
}
}
rayPosted Apr 7, 2006, 1:45 PM
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
Adam StaPosted Mar 31, 2006, 1:17 PM
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?
GrzegorzPosted Mar 31, 2006, 11:50 AM
try http://www.dotnet247.com/247reference/articles/1/8355.aspx
Hope it helps.
Regards and good luck,
Grek.