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

[Solved] convert radgrid field to date

2 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 29 Mar 2013, 06:12 PM
I have a date that I pull into my radgrid that I need to then convert to datefield so that I can color code them but whatever i do it never seems to work.  how can I change this string to date.

Protected Sub myRadGridList_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGridList.ItemDataBound
       If TypeOf e.Item Is GridDataItem Then
           Dim Item As GridDataItem = CType(e.Item, GridDataItem)
           Dim DOB As DateTime = DateTime.Parse(Item.Cells(2).Text.ToString)
 
           If DatePart(DateInterval.Month, DOB) = DatePart(DateInterval.Month, Date.Now) Then
               Item.Cells(2).ForeColor = Drawing.Color.Firebrick
           End If
       End If
   End Sub

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Mar 2013, 07:47 PM
Hello,

Please use Culture to convert string to datetime.

For Ex:

var dateString = "20081119";
var enUS = new System.Globalization.CultureInfo("en-US");
var resultingDate = DateTime.ParseExact(dateString,"yyyyMMdd",enUS);
Console.WriteLine(resultingDate.ToString());


Thanks,
Jayesh Goyani
0
Princy
Top achievements
Rank 2
answered on 01 Apr 2013, 05:16 AM
Hi,

The column index starts at index 2 because GridExpandColumn and GridRowindicatorColumn are always in front of data columns. So please make sure that you are accessing the correct column. One suggestion is to use the UniqueName property to access the cell.

VB:
Protected Sub myRadGridList_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGridList.ItemDataBound
 If TypeOf e.Item Is GridDataItem Then
  Dim Item As GridDataItem = CType(e.Item, GridDataItem)
  Dim cell As TableCell = DirectCast(Item("UniqueName"), TableCell)'Accessing the cell using the UniqueName
   . . .
   . . .
 End If
End Sub

You can perform the DateTime conversion as Jayesh suggested.

Thanks,
Princy.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or