Conditional text in a column

6 posts, 0 answers
  1. Viktor Takacs
    Viktor Takacs avatar
    95 posts
    Member since:
    Mar 2006

    Posted 22 Jun 2011 Link to this post

    Hi,
    I was wondering how I can change the text of a field in the grid based on it's value.In my scenario I have a Date field, but when its value is less than a predefined date I want to display an empty cell rather than the value.
    Is this possible?
    Thanks for any pointers!
  2. Rafal Tynski
    Rafal Tynski avatar
    13 posts
    Member since:
    Oct 2009

    Posted 22 Jun 2011 Link to this post

    You can create method and return string with any value.
    <ItemTemplate>
            <%# Action(Eval("DB_Field"))%>
    </ItemTemplate>

    If you can return empty string you must return &nbsp;
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Viktor Takacs
    Viktor Takacs avatar
    95 posts
    Member since:
    Mar 2006

    Posted 22 Jun 2011 Link to this post

    HI Rafal,

    Thanks for the quick reply.
    Can you please elaborate on this? I don't understand how to make that method.
    I need something like this (pseudo code):
    IF Eval("OrderDate") < Date.Today()
    RETURN "&nbsp;"

    Can you jot down a working example for me? I think it shouldn't be more than a line...Thank you so much!
  5. Rafal Tynski
    Rafal Tynski avatar
    13 posts
    Member since:
    Oct 2009

    Posted 22 Jun 2011 Link to this post


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="MRPRINT.COM.PL.WWW.Portal.Test" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="smMain">
        </asp:ScriptManager>
        <telerik:RadGrid runat="server" ID="rgMain" OnNeedDataSource="rgMain_NeedDataSource"
            AutoGenerateColumns="false">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn DataField="Date" HeaderText="Date">
                        <ItemTemplate>
                            <%# CDate(Eval("Date"))  %>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
     
    namespace MRPRINT.COM.PL.WWW.Portal
    {
        public class Test2
        {
            public int ID { get; set; }
     
            public string Name { get; set; }
     
            public DateTime Date { get; set; }
        }
     
        public partial class Test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
     
            public string CDate(object sender)
            {
                DateTime dtc = new DateTime(2010, 1, 1);
                DateTime dt = (DateTime)sender;
                if (dt < dtc)
                {
                    return " ";
                }
                else
                {
                    return dt.ToString();
                }
            }
     
            protected void rgMain_NeedDataSource(object sender, EventArgs e)
            {
                List<Test2> ls = new List<Test2>();
                ls.Add(new Test2() { ID = 1, Name = "R1", Date = new DateTime(2009, 1, 1) });
                ls.Add(new Test2() { ID = 2, Name = "R2", Date = new DateTime(2009, 6, 1) });
                ls.Add(new Test2() { ID = 3, Name = "R3", Date = new DateTime(2010, 1, 1) });
                ls.Add(new Test2() { ID = 4, Name = "R4", Date = new DateTime(2011, 1, 1) });
                rgMain.DataSource = ls;
            }
        }
    }


  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Jun 2011 Link to this post

    Hello Victor,

    Try the following code snippet in ItemDataBound event to show an empty cell when value is less than the predefined date

    VB:
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim cell As TableCell = item("ColumnUniqueName")
            If cell.ToString() ="date value" Then
                cell.Text = ""
            End If
        End If
    End Sub

    Thanks,
    Princy.
  7. Viktor Takacs
    Viktor Takacs avatar
    95 posts
    Member since:
    Mar 2006

    Posted 22 Jun 2011 Link to this post

    Ah, this is it, I see now. Thank you a lot!

    I'm going to use Rafal's suggestion as I feel it's more flexible for the future. With this custom function I can display anything I wanted.
    Thanks again to both of you!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017