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;
}
}
19 Answers, 1 is accepted
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
I try your code:
private
void
ResultGridView_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
if
(ResultGridView.CurrentColumn.Name ==
"Time"
)
{
e.EditorType=
typeof
(GridTimePickerEditor);
}
}
//ResultGridView.Columns["Time"].DataTypeConverter = new DateToTimeConverter();
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
I try to set frormatting:
ResultGridView.Columns[
"Time"
].FormatString =
"{0:HH:mm:ss.ff}"
;
//ResultGridView.Columns["Time"].FormatString = "{0:HH:mm:ss.ff}";
ResultGridView.Columns[
"Time"
].DataTypeConverter =
new
DateToTimeConverter();
private
void
ResultGridView_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
if
(ResultGridView.CurrentColumn.Name ==
"Time"
)
{
e.EditorType =
typeof
(GridTimePickerEditor);
}
}
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
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
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 {
get
;
set
; }
public
GridTimeSpanDataSource(TimeSpan time)
{
DateTime = DateTime.MinValue.Add(time);
}
}
Best Regards,
Emanuel Varga
WinForms MVP
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)
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
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."
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:
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
- When I can see this changes?
- 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'."?
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
What about my feature request?
Thanks
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
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 >>
Can we restrict user from choosing hour including AM/PM elapsed already in GridTimePickerEditor as we can restrict in date picker using MinValue?
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.
Dimitar
Telerik
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!
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.
Dimitar
Telerik