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

DateTime Column Formatting

4 Answers 2974 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 25 Mar 2015, 07:17 PM
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?

4 Answers, 1 is accepted

Sort by
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

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Mar 2015, 10:38 AM
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:
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.
 
Regards,
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:

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
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Apr 2015, 10:24 AM
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?
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.

 
Tags
GridView
Asked by
Phillip
Top achievements
Rank 1
Answers by
Mahmoud
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Phillip
Top achievements
Rank 1
Share this question
or