How can I bind ItemStyle to a data value?

3 posts, 1 answers
  1. Ritchie
    Ritchie avatar
    4 posts
    Member since:
    Oct 2007

    Posted 19 Feb 2008 Link to this post

    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. Ritchie
    Ritchie avatar
    4 posts
    Member since:
    Oct 2007

    Posted 19 Feb 2008 Link to this post

    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;
    
    
    
    }
    }
    }

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Shaun Peet
    Shaun Peet avatar
    571 posts
    Member since:
    Aug 2004

    Posted 19 Feb 2008 Link to this post

    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>

Back to Top