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

Conditional text in a column

5 Answers 474 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Viktor Takacs
Top achievements
Rank 2
Viktor Takacs asked on 22 Jun 2011, 10:09 AM
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!

5 Answers, 1 is accepted

Sort by
0
TomaszRabiasz
Top achievements
Rank 1
answered on 22 Jun 2011, 10:33 AM
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;
0
Viktor Takacs
Top achievements
Rank 2
answered on 22 Jun 2011, 10:52 AM
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!
0
TomaszRabiasz
Top achievements
Rank 1
answered on 22 Jun 2011, 12:13 PM

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


0
Princy
Top achievements
Rank 2
answered on 22 Jun 2011, 12:18 PM
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.
0
Viktor Takacs
Top achievements
Rank 2
answered on 22 Jun 2011, 12:24 PM
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!
Tags
Grid
Asked by
Viktor Takacs
Top achievements
Rank 2
Answers by
TomaszRabiasz
Top achievements
Rank 1
Viktor Takacs
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or