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

Format Date Column in Radgridview

5 Answers 616 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Xavier Soares
Top achievements
Rank 2
Xavier Soares asked on 09 Jun 2008, 01:32 PM

Hello,

I´´m trying to insert a column in Radgridview in winforms using the following code:

Dim
dateTimeColumn As New GridViewDateTimeColumn()

dateTimeColumn.UniqueName =

"DateTimeColumn"

dateTimeColumn.HeaderText =

"Test"

dateTimeColumn.FieldName =

"TestDate"

dateTimeColumn.FormatString =

"{0:MM/dd/yy}"

RadGridProjects.MasterGridViewTemplate.Columns.Add(dateTimeColumn)

I also tried do insert this column in design mode, but, the column always appear with the format 01/01/2000 02 00:00:00 instead of 01/01/2000.

What am I doing wrong?

Thank You
LM

5 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 12 Jun 2008, 02:00 PM
Hi Luis,

Thank you for writing.

Currently, there is an issue with FormatString for unbound columns. It will be addressed in one of our future releases.

For the time being, you can implement the functionality in your own code. Assign raw date values to the RadGridView at the state of binding. I used the following binding scheme:

            GridViewDataColumn dataCol1 = new GridViewDataColumn();  
            GridViewDataColumn dataCol2 = new GridViewDataColumn();  
            GridViewDateTimeColumn timeCol3 = new GridViewDateTimeColumn();  
 
            this.radGridView1.Columns.Add(dataCol1);  
            this.radGridView1.Columns.Add(dataCol2);  
            this.radGridView1.Columns.Add(timeCol3);  
 
            DateTime dt = DateTime.Now;  
 
            for (int i = 0; i < 100; ++i)  
            {  
                dt = dt.AddDays(1);  
                this.radGridView1.Rows.Add(  
                    new object[] { "a", i.ToString(), dt });  
            } 

In order to apply the formatting to the date, you only need handle the CellFormatting event as shown below:

        radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);  
 
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
        {  
            //execute the code only for the datetime column  
            //in this case it bears an index of 2  
            if (e.CellElement.ColumnIndex == 2)  
            {  
                //only apply formatting to unformatted values  
                if (!e.CellElement.Text.StartsWith("0:"))  
                {  
                    DateTime temp = DateTime.Parse(e.CellElement.Text);  
                    e.CellElement.Text = temp.ToString("{0:MM/dd/yy}");  
                }  
            }  
        } 

I hope this helps. If you have additional questions, feel free to contact me.

Best wishes,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Xavier Soares
Top achievements
Rank 2
answered on 12 Jun 2008, 05:03 PM
Thanks for your answer but i've changed the sql query to 

Convert(VARCHAR(10), TABQOT_Proposals.EntryDate , 111)

This solved my problem.

Thank You.
LM

0
Nikolay
Telerik team
answered on 13 Jun 2008, 08:49 AM
Hello Luis,

I am glad to hear that the issue is resolved.

If you have additional questions, do not hesitate to contact me.

Kind regards,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Deepa
Top achievements
Rank 1
answered on 19 Mar 2021, 06:04 PM
Did you ever get around to fix it? I see this issue existed back in 2008. I have tried to change the input data but the grid data still displays the date with time which I do not want
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Mar 2021, 10:23 AM
Hello, Deepa, 

I would like to note that this is quite an old post since 2008 and RadGridView has been majorly refactored during the years. If you have any specific setup in your grid, it would be greatly appreciated if you can share it with us. Thus, we would get better understanding of the precise case.

RadGridView offers a GridViewDateTimeColumn which provides date entry and formatting for DateTime data types. This column has a FormatString property that specifies cell's format and Format property that specifies the editor's format. The following forum post demonstrates how to achieve different formatting for the cells in editor mode and not in edit mode: https://www.telerik.com/forums/gridviewdatetimecolumn-need-date-without-time#z0wEIqLyJU6vqtuKRr-f5g 

The following help article gives more information about how to setup the GridViewDateTimeColumn: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/column-types/gridviewdatetimecolumn 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Xavier Soares
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Xavier Soares
Top achievements
Rank 2
Deepa
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or