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

Best Approach for DateTime.MinValue with Grid

3 Answers 256 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marcelo Serragnoli
Top achievements
Rank 1
Marcelo Serragnoli asked on 21 Dec 2010, 05:48 PM

Hello, this is my first post here at Telerik, I tried to find here some solution but without success.

I have a Value Object (VO) which I assign to the gridView DataSource, all the data is showed by the gridView! The problem starts when inside the VO I assign a dateTime as MinValue the gridView shows "01/01/0001", now my question is which is the best approach to show " " instead of "01/01/0001" ?

Thanks,
Marcelo Serragnoli

3 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Dec 2010, 12:41 PM
Hi Marcelo,

Welcome to the forums. I think your best approach here would be to use the CellFormatting event of the RadGridView to inpect the cell's value, and change the text of the cell if it is that date.

for example, add this to your form load
Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("A"))
Me.RadGridView1.Columns.Add(New GridViewDateTimeColumn("B"))
Dim rowInfo As GridViewRowInfo = Me.radGridView1.Rows.AddNew()
rowInfo.Cells(0).Value = "A1"
rowInfo.Cells(1).Value = Now
rowInfo = Me.radGridView1.Rows.AddNew()
rowInfo.Cells("A").Value = "A2"
rowInfo.Cells("B").Value = New DateTime(1, 1, 1)
Me.RadGridView1.Columns(1).FormatString = "{0:d}"

and then handle the CellFormatting event
Private Sub RadGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
    If e.CellElement.ColumnInfo.Name = "B" Then
        If CDate(e.CellElement.Value) = New Date(1, 1, 1) Then
            e.CellElement.Text = ""
        End If
    End If
End Sub

You can find out more about CellFormatting at this documentation link

hope that helps
Richard
0
Marcelo Serragnoli
Top achievements
Rank 1
answered on 22 Dec 2010, 02:52 PM
Hi Richard,

Thanks for the help, the CellFormatting works great, but as I'm using the telerik Q1 2010 I did some changes because my "e.CellElement.ColumnInfo" doesn't have the property "Name".

Could you please just confirm if the code below is the best way.

private void radGridViewTrade_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridViewDataColumn column = e.CellElement.ColumnInfo as GridViewDataColumn;
            if (column.UniqueName.Equals("gridTrade_Redemption"))
            {
                if (e.CellElement.Text != string.Empty &&
          Convert.ToDateTime(e.CellElement.Text) == DateTime.MinValue)
                {
                    e.CellElement.Text = "";
                }
            }
        }

Thanks again.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Dec 2010, 02:59 PM
Hi,

Yes, using UniqueName will be fine. As long as your not using ColumnIndex = Something as this could change when you re-order columns.
Please remember to mark as answer and let me know if you need anything else
Richard
Tags
GridView
Asked by
Marcelo Serragnoli
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Marcelo Serragnoli
Top achievements
Rank 1
Share this question
or