I want to format a datetime column specifically as MM/DD/YYYY.
I am able to modify the display date values as mm/dd/yyyy, but when the user attempted to edit the data, it goes back into long date format.
Is there a way to change the entry of the value to that format?
I am able to modify the display date values as mm/dd/yyyy, but when the user attempted to edit the data, it goes back into long date format.
Is there a way to change the entry of the value to that format?
4 Answers, 1 is accepted
0
Mahmoud
Top achievements
Rank 1
answered on 29 Mar 2015, 09:03 AM
hi Phillip
see http://www.telerik.com/help/winforms/gridview-columns-data-formatting.html
And user {0:d} in format string from Date
see http://www.telerik.com/help/winforms/gridview-columns-data-formatting.html
And user {0:d} in format string from Date
0
Hello,
Thank you for writing.
The GridViewDateTimeColumn.FormatString property sets the format of the date when the date is not currently being edited. The GridViewDateTimeColumn.CustomFormat property is used to format the date once the user clicks on the cell to invoke the editor. Please refer to our GridViewDateTimeColumn help article:
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Dess
Telerik
Thank you for writing.
The GridViewDateTimeColumn.FormatString property sets the format of the date when the date is not currently being edited. The GridViewDateTimeColumn.CustomFormat property is used to format the date once the user clicks on the cell to invoke the editor. Please refer to our GridViewDateTimeColumn help article:
GridViewDateTimeColumn dateTimeColumn =
new
GridViewDateTimeColumn(
"DateTimeColumn"
);
dateTimeColumn.FormatString =
"{0:MM/dd/yyyy}"
;
dateTimeColumn.Format = DateTimePickerFormat.Custom;
dateTimeColumn.CustomFormat =
"MM/dd/yyyy"
;
radGridView1.MasterTemplate.Columns.Add(dateTimeColumn);
I hope this information helps. Should you have further questions, I would be glad to help.
Dess
Telerik
See What's Next in App Development. Register for TelerikNEXT.
0
Phillip
Top achievements
Rank 1
answered on 30 Mar 2015, 03:52 PM
Dess,
I attempted to use your solution, but it is not working in my scenario. I am using a databound grid and therefore need to modify the grid after binding. This is the code I'm using:
This approach does not successfully change the format for editing or non-editing. In order for me to get even the non editing method to work, I had to do the following:
Suggestions?
I attempted to use your solution, but it is not working in my scenario. I am using a databound grid and therefore need to modify the grid after binding. This is the code I'm using:
private
void
gvData_DataBindingComplete_Vehicles(
object
sender, GridViewBindingCompleteEventArgs e)
{
for
(
int
j = 0; j < gvData.Columns.Count; j++)
{
if
(gvData.Columns[j].GetType() ==
typeof
(Telerik.WinControls.UI.GridViewDateTimeColumn))
{
((GridViewDateTimeColumn)gvData.Columns[j]).FormatString =
"{0:MM/dd/yyyy}"
;
((GridViewDateTimeColumn)gvData.Columns[j]).Format = DateTimePickerFormat.Custom;
((GridViewDateTimeColumn)gvData.Columns[j]).CustomFormat =
"MM/dd/yyyy"
;
}
}
}
This approach does not successfully change the format for editing or non-editing. In order for me to get even the non editing method to work, I had to do the following:
private
void
gvData_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
try
{
if
(e.Column
is
GridViewDateTimeColumn && e.CellElement.Value !=
null
&& e.Column.Name !=
"updatedon"
)
{
DateTime value = DateTime.Parse(e.CellElement.Value.ToString());
e.CellElement.Text = value.ToShortDateString();
}
}
catch
{ }
}
Suggestions?
0
Hello Phillip,
Thank you for writing back.
Following your description, I have tried to reproduce the issue you are facing with the specified version 2015.1 225 but without any success. Here is my sample code snippet. Could you please have a look at it and specify how it differs from yours?
It would be greatly appreciated if you can provide a sample code snippet which replicates the issue in the latest version. Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.
I am looking forward to your reply.
Regards,
Dess
Telerik
Thank you for writing back.
Following your description, I have tried to reproduce the issue you are facing with the specified version 2015.1 225 but without any success. Here is my sample code snippet. Could you please have a look at it and specify how it differs from yours?
public
Form1()
{
InitializeComponent();
List<Item> items =
new
List<Item>();
for
(
int
i = 0; i < 10; i++)
{
items.Add(
new
Item(i,i +
"Item"
, DateTime.Now.AddDays(i)));
}
this
.radGridView1.DataBindingComplete += radGridView1_DataBindingComplete;
this
.radGridView1.DataSource = items;
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
private
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
for
(
int
j = 0; j <
this
.radGridView1.Columns.Count; j++)
{
if
(
this
.radGridView1.Columns[j].GetType() ==
typeof
(Telerik.WinControls.UI.GridViewDateTimeColumn))
{
((GridViewDateTimeColumn)
this
.radGridView1.Columns[j]).FormatString =
"{0:MM/dd/yyyy}"
;
((GridViewDateTimeColumn)
this
.radGridView1.Columns[j]).Format = DateTimePickerFormat.Custom;
((GridViewDateTimeColumn)
this
.radGridView1.Columns[j]).CustomFormat =
"MM/dd/yyyy"
;
}
}
}
public
class
Item
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
DateTime CreatedOn {
get
;
set
; }
public
Item(
int
id,
string
name, DateTime createdOn)
{
this
.Id = id;
this
.Name = name;
this
.CreatedOn = createdOn;
}
}
It would be greatly appreciated if you can provide a sample code snippet which replicates the issue in the latest version. Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.
I am looking forward to your reply.
Regards,
Dess
Telerik
See What's Next in App Development. Register for TelerikNEXT.