开发者

ASP.NET 3.5 GridView Template Field Display Date If Not MinValue?

I have a rather simple problem with ASP.NET. I have a grid view with a template field. In the template field I have a label. I'm trying to set the labels text property to the date specified in a bound column if the datetime value is not the min value (in other words the date time field hasn't been set). I'm really not sure about how to go about this. I've given some code I've tried below, to be more clear in what I'm trying to accomplish:

        <asp:TemplateField HeaderText="Start Date开发者_如何学Python">
            <ItemTemplate>
                <asp:Label ID="startDateLabel" runat="server"
                    Text='<%# if (Eval("StartDate") == DateTime.MinValue) { "None" } else { Eval("StartDate") } %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

Here is the code for my object data source as requested:

<asp:ObjectDataSource ID="projectDataSource" runat="server" 
    SelectMethod="GetProjects" TypeName="Lemur.Services.Impl.ProjectService">
</asp:ObjectDataSource>

Here is the class that is being returned from the object data source:

public class Project
{
    #region Private Fields

    private Client _client;

    #endregion

    #region Public Properties

    public Guid ID { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public string Description { get; set; }
    public Guid ClientID { get; set; }
    public Client Client
    {
        get
        {
            if (_client == null)
            {
                _client = new Client();
            }

            return _client;
        }
        set
        {
            _client = value;
        }
    }
    public ProjectStatus ProjectStatus { get; set; }
    public Employee ProjectManager { get; set; }
    public Guid ProjectManagerID { get; set; }
    public bool Active { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    #endregion

    #region Overrides

    public override string ToString()
    {
        return Name;
    }

    #endregion
}

The field I am trying to retrieve is StartDate.

Anyone have any ideas on how to accomplish this?

Thanks for any help.


After updating my post with the Project class code I realized that trying to integrate an if statement into the TemplateField seemed sloppy. I decided to add a property to the Project class called StartDateString, which contains the logic I wanted to perform at the view layer. This way I can just create a Bound field to the StartDateString property:

    public string StartDateString
    {
        get
        {
            if (StartDate == DateTime.MinValue)
            {
                return "";
            }
            else
            {
                return StartDate.ToString("dd/MM/yyyy");
            }
        }
    }

Guess I was approaching the problem from the wrong direction....


I realise this post is old, but I just had the same issue (have done so in the past but I'd forgotten how to solve it). Found solution elsewhere put thought might be of use for others seeking the same. Anyhow....

Use a nullable DataTime type in your class. "DateTime?" instead of "DateTime"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜