This is a migrated thread and some comments may be shown as answers.

How can I bind ItemStyle to a data value?

2 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ritchie
Top achievements
Rank 1
Ritchie asked on 20 Feb 2008, 01:12 AM
This is a simple request:

How should I go about binding the style of an Item in a datagrid to a value? For an obvious example, I'd like to set negative values in a column to red and positive to green. This is using RadGrid and whichever column type suits the task best. My data is coming from an arbitrary data source.

I assume this is possible, but I'd like to know the simplest way to do it.

Thanks

2 Answers, 1 is accepted

Sort by
0
Ritchie
Top achievements
Rank 1
answered on 20 Feb 2008, 01:31 AM

Ah, nevermind. I was hoping for a way to do it through the designer UI, but instead I just implemented it in code behind...



protected void RadGrid1_DataBound(object sender, EventArgs e)

{

foreach (GridDataItem item in RadGrid1.Items)
{


TableCell cell = item["Diff"];


if (item["Status"].Text == "0"){

cell.ForeColor = 
Color.Red;

}

else

{
cell.ForeColor = 
Color.Green;

}
}
}

0
Accepted
Shaun Peet
Top achievements
Rank 2
answered on 20 Feb 2008, 02:37 AM
You could also use a GridTemplateColumn, and then in the <ItemTemplate> have two labels:

<ItemTemplate>
<asp:Label runat="server" id="lblGreen" ForeColor="Green" Text='<%# Eval("FieldName") %>' Visible='<%# Iif(Eval("FieldName") = "0", False, True) %>' />
<asp:Label runat="server" id="lblRed" ForeColor="Red" Text='<%# Eval("FieldName") %>' Visible='<%# Iif(Eval("FieldName") = "0", True, False) %>' />
</ItemTemplate>

Tags
Grid
Asked by
Ritchie
Top achievements
Rank 1
Answers by
Ritchie
Top achievements
Rank 1
Shaun Peet
Top achievements
Rank 2
Share this question
or