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

GridTimePickerEditor for edit hour, minute, second and millisecond

19 Answers 433 Views
GridView
This is a migrated thread and some comments may be shown as answers.
rent
Top achievements
Rank 1
rent asked on 19 Nov 2012, 08:57 AM
Nice coding! I've following question:
In my previos in Time field I make this for editing:
ResultGridView.Columns["Time"].HeaderText = "Time";
            ResultGridView.Columns["Time"].DataTypeConverter = new DateToTimeConverter();
...
private void ResultGridView_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (ResultGridView.CurrentColumn.Name == "Time")
            {
                e.Editor = new GridTimePickerEditor();
            }
        }
...
public class DateToTimeConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(DateTime);
        }
 
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            TimeSpan span = (TimeSpan)value;
            DateTime dt = new DateTime(span.Ticks);
            return dt;
        }
 
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(DateTime);
        }
 
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            DateTime dt = (DateTime)value;
            TimeSpan span = new TimeSpan(dt.Hour, dt.Minute, dt.Second);
            return span;
        }
    }
Now I can edit only hour and minute, it possible to edit seconds and milliseconds too?

19 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Nov 2012, 10:34 AM
Hello Rent,

You should be able to set the format of the column to the desired one and the editor should respect that format, just do not return a new value in the EditorRequired, instead just pass the type of the editor, like so:

private void ResultGridView_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (ResultGridView.CurrentColumn.Name == "Time")
            {
                e.EditorType= typeof(GridTimePickerEditor);
            }
        }

I will take a look this afternoon at the entity example (sorry, no db right now) but i can tell you that the issue you are having is not related to that approach

Best Regards,
Emanuel Varga
WinForms MVP
0
rent
Top achievements
Rank 1
answered on 19 Nov 2012, 11:04 AM
Hello Emanuel!
I try your code:
private void ResultGridView_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (ResultGridView.CurrentColumn.Name == "Time")
            {
                e.EditorType= typeof(GridTimePickerEditor);
            }
        }
And comment this:
//ResultGridView.Columns["Time"].DataTypeConverter = new DateToTimeConverter();
And I can't get worked for me - only edit hour and second. How to fix this?
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Nov 2012, 11:08 AM
Hello again,,

You forgot about this one
"You should be able to set the format of the column to the desired one"

Maybe i wasn't to specific here i saw that you've switched to a DateTimeColumn so, you should set the FormatString of the column to a custom one to include the seconds in the format. You can find more info on formats on msdn.

Best Regards,
Emanuel Varga
WinForms MVP
0
rent
Top achievements
Rank 1
answered on 19 Nov 2012, 11:28 AM
Thanks for reply Emanuel!
I try to set frormatting:
ResultGridView.Columns["Time"].FormatString = "{0:HH:mm:ss.ff}";
But now I get error: Input string was not in a correct format. What I did wrong?
0
rent
Top achievements
Rank 1
answered on 19 Nov 2012, 11:42 AM
Without this in form load:
//ResultGridView.Columns["Time"].FormatString = "{0:HH:mm:ss.ff}";
            ResultGridView.Columns["Time"].DataTypeConverter = new DateToTimeConverter();
I get error  Input string was not in a correct format. Editor code I changed to:
private void ResultGridView_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (ResultGridView.CurrentColumn.Name == "Time")
            {
                e.EditorType = typeof(GridTimePickerEditor);
            }
        }
Any solution?
I forrgot to say that in Database Time field type is TIME(2) - to store a little time intervals like 05:45:34.45 {HH:mm:ss.ff} and in EF model this field type is Time
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Nov 2012, 12:02 PM
Hello again,

I've just tested this, you can use the following:
private void grid_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    var dateTimeColumn = grid.Columns["DateTime"] as GridViewDateTimeColumn;
    if (dateTimeColumn != null)
    {
        dateTimeColumn.FormatString = "{0:HH:mm:ss.ff}";
    }
}
 
void grid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    var editor = e.ActiveEditor as GridTimePickerEditor;
    if (editor != null)
    {
        var editorElement = editor.EditorElement as RadTimePickerElement;
        if (editorElement != null)
        {
            editorElement.Format = "HH:mm:ss.ff";
            editorElement.Value = e.Value;
        }
    }
}

You can use this only for DateTime columns, if your column is not a datetime column you can change the appearance of the cell in the way i specified in the other post.

Also there is a bug here, because the format of the TimePickerElement is not correct when setting a FormatString on the column (as for the workaround in CellEditorInitialized, and one other thing, changing the Format for the element, caused a reset of the Value of the editorElement to the current date and time.

Best Regards,
Emanuel Varga
WinForms MVP
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Nov 2012, 12:14 PM
I've submitted a bug report on these issue(s)

The contents are as following (if anyone else is interested)

Hello,

The issues are as following:
- The editors format is not correctly taken from the columns FormatString
- The custom format does not work (documentation)
- The value of the editor element resets when changing the Format.

Following this thread i've prepared an example which reproduces this issue(s) clearly
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView grid = new RadGridView();
 
    public Form1()
    {
        InitializeComponent();
        grid.Dock = DockStyle.Fill;
        grid.EditorRequired += grid_EditorRequired;
        grid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        grid.DataBindingComplete += grid_DataBindingComplete;
 
        this.Controls.Add(grid);
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
 
        var list = new List<GridTimeSpanDataSource>();
        for (int i = 0; i < 3; i++)
        {
            list.Add(new GridTimeSpanDataSource(new TimeSpan(9, i, 0)));
        }
 
        grid.DataSource = list;
        //grid.CellEditorInitialized += grid_CellEditorInitialized;
    }
 
    private void grid_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        var dateTimeColumn = grid.Columns["DateTime"as GridViewDateTimeColumn;
        if (dateTimeColumn != null)
        {
            dateTimeColumn.FormatString = "{0:HH:mm:ss.ff}";
 
            // also this does not work...
            dateTimeColumn.Format = DateTimePickerFormat.Custom;
            dateTimeColumn.CustomFormat = "t";
        }
    }
 
    /// <summary>
    /// Workaround because of the current issue
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void grid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        var editor = e.ActiveEditor as GridTimePickerEditor;
        if (editor != null)
        {
            var editorElement = editor.EditorElement as RadTimePickerElement;
            if (editorElement != null)
            {
                editorElement.Format = "HH:mm:ss.ff";
                // this should not be required, because i don't see why the format of the element should cause a value reset..
                editorElement.Value = e.Value;
            }
        }
    }
 
    private void grid_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (grid.CurrentColumn.FieldName == "DateTime")
        {
            e.EditorType = typeof(GridTimePickerEditor);
        }
    }
}
 
public class GridTimeSpanDataSource
{
    public DateTime DateTime { getset; }
 
    public GridTimeSpanDataSource(TimeSpan time)
    {
        DateTime = DateTime.MinValue.Add(time);
    }
}

Best Regards,
Emanuel Varga
WinForms MVP
0
rent
Top achievements
Rank 1
answered on 19 Nov 2012, 12:16 PM
Thanks Emanuel for reply!
So, to solve my problem I must use DateTime in database and in Ef model? But I need to store hour, minute, seconds and milliseconds (HH:mm:ss.ff) (It's competiton of swiming table)
0
Emanuel Varga
Top achievements
Rank 1
answered on 19 Nov 2012, 12:28 PM
Hello,

No you don't, but you can do the following, remove the autogenerated column and add another datetime column, like so:
private void grid_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    // remove the autogenerated column
    grid.Columns.Remove("Time");
 
    // create the new column
    var dateTimeColumn = new GridViewDateTimeColumn("Time")
        {
            DataTypeConverter = new DateToTimeConverter(),
            DataType = typeof(DateTime),
            FormatString = "{0:HH:mm:ss.ff}"
        };
    grid.Columns.Add(dateTimeColumn);
}

Best Regards,
Emanuel Varga
WinForms MVP
0
rent
Top achievements
Rank 1
answered on 20 Nov 2012, 03:50 AM
Hi Emanuel!
On your example I've got error: "SqlDbType.Time overflow.  Value '734826.07:48:42' is out of range.  Must be between 00:00:00.0000000 and 23:59:59.9999999." 
0
Peter
Telerik team
answered on 22 Nov 2012, 06:47 AM
Hi Rent,
I would like to clarify that our RadDateTimePicker editor does not support milliseconds editing. I would suggest that you change the format to just seconds, without milliseconds:
Copy Code
if (editorElement != null)
{                   
          editorElement.Format = "HH:mm:ss";

I have logged this as a feature request.

We not able to reproduce the exception without your project and database - Could you please open a new support ticket where you can send to us a build-able project with database file where the issue can be reproduced? 

I am looking forward your response.

Kind regards,
Peter
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
rent
Top achievements
Rank 1
answered on 26 Nov 2012, 08:12 AM
Hello Peter!
  1. When I can see this changes?
  2. If I use RadMaskedEditBoxEditor, why I can not edit milliseconds part of time in mask "HH:mm:ss.ff" and I get error: "Invalid cast from 'System.DateTime' to 'System.TimeSpan'."?
Thanks.
0
Peter
Telerik team
answered on 29 Nov 2012, 07:36 AM
Hello Rent,

Thank you for writing back.

Do to the nature of the request I am not able to engage with any specific time frame. As we clarified our RadDateTimePicker editor does not support milliseconds editing - this is logged as a feature request: http://www.telerik.com/support/pits.aspx#/public/winforms/13515.

I was not able to reproduce the described behavior without your project and database. It is a long shot, but I would suggest to change the TimeSpan in the Bussnes object to DateTime:
public class GridTimeSpanDataSource
{
    public DateTime DateTime { get; set; }
  
    public GridTimeSpanDataSource(DateTime time)
    {
        DateTime = time;
    }
}

and 
for (int i = 0; i < 3; i++)
{
            list.Add(new GridTimeSpanDataSource(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)));
}

I hope this helps. Kind regards,
Peter
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
rent
Top achievements
Rank 1
answered on 17 Sep 2013, 11:58 AM
Hello!

What about my feature request?

Thanks
0
Nikolay
Telerik team
answered on 18 Sep 2013, 10:06 AM
Hi rent,

Your feature request has not been scheduled for implementation yet. We will increase its priority as it gets more votes. You can see our current WinForms Roadmap here.

Regards,
Nikolay
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Chandrak
Top achievements
Rank 1
answered on 18 Aug 2015, 09:22 AM

Can we restrict user from choosing hour including AM/PM elapsed already in GridTimePickerEditor as we can restrict in date picker using MinValue?

 

0
Dimitar
Telerik team
answered on 20 Aug 2015, 01:23 PM
Hi ,

Thank you for writing.

The time picker has MinValue as well. You can set it in the CellEditorInitialized event of the grid:
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    GridTimePickerEditor editor = e.ActiveEditor as GridTimePickerEditor;
 
    if (editor != null)
    {
        var element = editor.EditorElement as RadTimePickerElement;
        element.MinValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 30, 0);
         
       
    }
}

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
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
0
Chandrak
Top achievements
Rank 1
answered on 20 Aug 2015, 02:30 PM

oopss.. I missed it! Thanks I've used cellvelidated event and datetime function to restrict user. 

One more question..

 my application launches lot of forms in rad dock. When exiting application I need to inform each of those forms for some cleanup work and release ongoing threads in them. is there any way to send some message from main application form (MDI container) to each of them? number of forms to be opened is not limited. I give random names to document windows (like one form can be launched number of times by user so I am assigning random values to the name property)

 Thanks again!

0
Dimitar
Telerik team
answered on 25 Aug 2015, 07:01 AM
Hi ,

Thank you for writing back.

You can use the MdiChildren collection to access the child windows:
private void radButton1_Click(object sender, EventArgs e)
{
    foreach (var item in radDock1.MdiChildren)
    {
        Console.WriteLine(item.Text);
    }
}

I hope this will be useful.
 
Regards,
Dimitar
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
Tags
GridView
Asked by
rent
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
rent
Top achievements
Rank 1
Peter
Telerik team
Nikolay
Telerik team
Chandrak
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or