Edit TimeSpan

10 posts, 1 answers
  1. Sergio Sisternes
    Sergio Sisternes avatar
    6 posts
    Member since:
    Nov 2009

    Posted 01 Mar 2010 Link to this post

    Hello!

    I have a little question about RadGridView editors.

    In SQL Server 2008-Entity Framework context, a Time datatype field is translated as a .NET TimeSpan class. I tried to edit this kind of fields in a RadGridView, but a cast exception is thrown.

    Is there any way to edit this data type in the GridView?

    Thank you so much.
  2. Nick
    Admin
    Nick avatar
    767 posts

    Posted 01 Mar 2010 Link to this post

    Hi Sergio Sisternes,

    Thank you for contacting us. The TimeSpan object cannot be edited through its properties e.g Tick as they are read-only (have only getters). Therefore, even if you write a custom editor for this type, you will not be able to change the values.

    I think that the right conversion of SQL type Time is DateTime in .net. I suggest that you find how to make that conversion in Entity Framework if the generated mapping is wrong. Please read further in MSDN.

    Do not hesitate to write me back if you have further questions.

    Best wishes,
    Nick
    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sergio Sisternes
    Sergio Sisternes avatar
    6 posts
    Member since:
    Nov 2009

    Posted 01 Mar 2010 Link to this post

    Hi Nick,

    Thank you for the fast response.

    No, the right conversion for SQL Server 2008 Time in Entity Framework 4 is TimeSpan. If I try to modify the type mapping, the compiler throws several errors saying that Edm.DateTime is not compatible with SqlServer.time

    Error    3    Error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' of member Time' in type 'Model.Registry' is not compatible with 'SqlServer.time[Nullable=False,DefaultValue=,Precision=0]' of member 'Time' in type 'Model.Store.Registries'.    DataModel.edmx    1161    13    Data Library


    Is there any possibility to handle this issue creating an editor that converts String text to TimeSpan?
  5. Sergio Sisternes
    Sergio Sisternes avatar
    6 posts
    Member since:
    Nov 2009

    Posted 02 Mar 2010 Link to this post

    Hi again Nick,

    You can find more information about SQL.Time at the following link:
    http://msdn.microsoft.com/en-us/library/cc716757.aspx

    "Time (EDM)

    The Time type represents an interval of time that can be added to or subtracted from an instance of the DateTime type in the Entity Data Model (EDM).

    This type maps to the TimeSpan type in the common language runtime (CLR) and to the Time type in SQL Server."

    Hoping we can find a solution,

    Best regards,
    Sergio

  6. Nick
    Admin
    Nick avatar
    767 posts

    Posted 02 Mar 2010 Link to this post

    Hello Sergio Sisternes,

    You are absolutely correct. Thank you for providing the link. I created a work-around for you.

    First you need to create a custom editor for your column where you need to parse the string value of the grid textbox column and write a TimeSpan to your database:

    class MyEditor : RadTextBoxEditor
           {
               public override object Value
               {
                   get
                   {
                       return base.Value;
                   }
                   set
                   {
                       base.Value = TimeSpan.Parse(value.ToString());
                   }
               }
           }

    Then you have to replace the standard text box editor with the new one in EditorRequired event:

    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
          {
              if (this.radGridView1.CurrentColumn.HeaderText == "TimeSpan")
              {
                  if (e.EditorType == typeof(RadTextBoxEditor))
                  {
                      e.Editor = new MyEditor();
                  }
              }
          }

    I hope that this helps. You may find more information about our editors in our online documentation.

    Greetings,
    Nick
    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.
  7. Sergio Sisternes
    Sergio Sisternes avatar
    6 posts
    Member since:
    Nov 2009

    Posted 02 Mar 2010 Link to this post

    Thank you a lot!

    But I have a last problem...

    I put the code exactly as you said (saving the difference of the column header).

    This time when I edit the TimeSpan I don't get any error, but the TimeSpan value does not change in grid or EDM context and the following messages are sent to the Debug console:

    at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)

    at Telerik.WinControls.Data.DataAccessComponent.set_Item(GridViewRowInfo row, GridViewDataColumn column, Object value)


    Only and only this 2 lines.

    Here follows my code:

        public class RadTimeSpanEditor : RadTextBoxEditor  
        {  
            public override object Value  
            {  
                get 
                {  
                    return base.Value;  
                }  
                set 
                {  
                    base.Value = TimeSpan.Parse(value.ToString());  
                }  
            }  
        } 



    private void gridView_EditorRequired(object sender, EditorRequiredEventArgs e)  
    {  
        if (gridView.CurrentColumn.HeaderText == "Registry") 
        {  
            if (e.EditorType == typeof(RadTextBoxEditor))  
            {  
                e.Editor = new RadTimeSpanEditor();  
            }  
        }  
    }  
     

    For this second block code, I checked that e.EditorType is RadTextBoxEditor in order to instance the RadTimeSpanEditor. In all cases, all code goes right but the Value change.
  8. Answer
    Nick
    Admin
    Nick avatar
    767 posts

    Posted 03 Mar 2010 Link to this post

    Hello Sergio Sisternes,

    Thank you for contacting us back. It is my error, here is the modified code:

    public class RadTimeSpanEditor : RadTextBoxEditor
        {
            public override object Value
            {
                get
                {
                    return TimeSpan.Parse(base.Value.ToString());
                }
                set
                {
                    base.Value = value.ToString();
                }
            }
        }

    And you need to call SaveChanges at the appropriate places for your applications.

    int result = entities.SaveChanges();

    Please excuse me for not providing the correct work-around the first time.

    Regards,
    Nick
    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.
  9. Sergio Sisternes
    Sergio Sisternes avatar
    6 posts
    Member since:
    Nov 2009

    Posted 03 Mar 2010 Link to this post

    Right!

    Worked perfectly.

    Thank you so much.
  10. Marco
    Marco avatar
    87 posts
    Member since:
    Apr 2011

    Posted 15 Jan Link to this post

    Would be nice if someday Telerik provide an editor for TimeSpan.

    I just share my version, which handle Nullable TimeSpan and keep only hour and minutes. And how to handle format error with the CellValidating Event.

    Private Sub aRadGridView_CellValidating(sender As System.Object, e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles aRadGridView.CellValidating
            If e.ActiveEditor IsNot Nothing AndAlso TypeOf e.ActiveEditor Is RadTimeSpanEditor Then
                Dim test As TimeSpan
                If TimeSpan.TryParse(e.Value, x) OrElse String.IsNullOrWhiteSpace(e.Value) Then
     
                Else
                    e.Cancel = True
                    Beep()
                End If
            End If
        End Sub

    Private Class RadTimeSpanEditor
        Inherits RadTextBoxEditor
     
        Public Overrides Property Value As Object
            Get
                Dim temp As TimeSpan
                If TimeSpan.TryParse(MyBase.Value.ToString, temp) Then
                    temp = New TimeSpan(temp.Hours, temp.Minutes, 0)
                    Return temp.ToString
                End If
                Return MyBase.Value
            End Get
            Set(value As Object)
                MyBase.Value = If(value Is Nothing, String.Empty, value.ToString())
            End Set
        End Property
    End Class

  11. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 18 Jan Link to this post

    Hello Marco,

    Thank you for sharing your solution with the community.

    We have a feature request for developing a GridTimeColumn which will work with a TimeSpan editor in RadGridView. Please make sure that you cast your vote to the public feedback item: ADD. RadGridView - New Grid Column - GridTimeColumn.

    I hope this information is useful. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready