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

Null date values in GridBoundColumn

4 Answers 1103 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 06 Nov 2010, 03:39 AM
Hi,

For displaying datetime values in a RadGrid column I am using a GridBoundColumn with DataType="System.DateTime" and DataFormatString="{0:M/d/yyyy}".  This works fine except in cases where the bound value is null (DateTime.MinVal) - the date value is displayed as "1/1/0001". 

I could define the column as a string and emit a blank string when the date field is set to DateTime.MinVal, but then I loose the capability of sorting on the date column (unless the date format is "yyyy/MM/dd".

Is there a way to leave the data type of the column as "DateTime" and also display an empty cell if the value of the date is DateTime.MinVal?

4 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 09 Nov 2010, 02:56 PM
Hello Richard,

To achieve the desired functionality you could try using the following code snippet into the RadGrid.ItemDataBound event handler:
void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        if (item["Date"].Text == DateTime.MinValue.ToString("M/d/yyyy"))
        {
            item["Date"].Text = "";
        }
    }
}

Please give it try and let me know if it helps you.

Greetings,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Envision
Top achievements
Rank 1
answered on 09 Nov 2010, 08:14 PM
The proposed solution is not working for linkbutton columns. 

The markup from the column is shown below and a graphic of the web page is attached:

    <telerik:GridButtonColumn
        ButtonType="LinkButton"
        UniqueName="AssignmentDate"
        CommandName="AssignmentDate" 
        DataTextField="AssignmentDate"
        DataTextFormatString="{0:M/d/yyyy}"
        DataType="System.DateTime"
        HeaderText="Assign Date"
        ItemStyle-HorizontalAlign="Center" />

0
Accepted
Dimo
Telerik team
answered on 11 Nov 2010, 08:41 AM
Hello,

The LinkButton's Text should be manipulated, instead of the TableCell's one:

Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso Not e.Item.IsInEditMode Then
            Dim cell As TableCell = DirectCast(e.Item, GridDataItem)("Date1")
            Dim lb As LinkButton = DirectCast(cell.Controls(0), LinkButton)
            If lb.Text = "1/1/0001" Then
                cell.Text = "&nbsp;"
            End If
    End If
End Sub


Regards,
Dimo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rich
Top achievements
Rank 1
answered on 11 Nov 2010, 09:36 PM
That fixed the problem - thank you
Tags
Grid
Asked by
Rich
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Envision
Top achievements
Rank 1
Dimo
Telerik team
Rich
Top achievements
Rank 1
Share this question
or