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

DateTime Value in gridview to String not in correct format

1 Answer 386 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lily
Top achievements
Rank 1
Veteran
Lily asked on 30 Jan 2020, 08:30 PM

Hi,

I'm having a hard time trying to get a datetime value from my gridview to string in the correct format that I have set to display on the gridview.

 

I want:   yyyy/MM/dd HH:ss ex. 2020/01/30 14:15

But the result keeps giving me 1/23/2020 2:05:08 PM

Then to string with all the non-alphanumeric removed it gives me: 1232020258PM   (noticed it removed any 0s in front of the mins and secs...

 

I have already formatted the datetime on the gridview to display the correct format. But the value to string still gives me the other format.

I've also tried to grab the data from Sql database instead of gridview, however I still get the same result.

 

Ultimately, I need the correct format in the string so that I can eventually take it apart again to put in a different table.  And why it will be in string is because i need to grab this info to include in the name of the PDF file.  Then I'll make a table using info from the name breakdown to pull the PDF to put info a table to select it.

Help?  Thank you!!!

My code:

private void radGridView1_CommandCellClick(object sender, GridViewCellEventArgs e)
       {
           plantCode = radGridView1.CurrentRow.Cells["plantCode"].Value.ToString();
           machine = radGridView1.CurrentRow.Cells["machine"].Value.ToString();
           ordNum = radGridView1.CurrentRow.Cells["productionOrderNum"].Value.ToString().Substring(0, 7);
           ordItem = radGridView1.CurrentRow.Cells["productionOrderNum"].Value.ToString().Substring(8, 3);
           apprStatus = radGridView1.CurrentRow.Cells["approvalList"].Value.ToString();
           custName = radGridView1.CurrentRow.Cells["addrName"].Value.ToString();
           process = radGridView1.CurrentRow.Cells["process"].Value.ToString();
           prodCode = radGridView1.CurrentRow.Cells["prodCode"].Value.ToString();
           custId = radGridView1.CurrentRow.Cells["custId"].Value.ToString();
           approvalTime = Regex.Replace(radGridView1.CurrentRow.Cells["approvalTime"].Value.ToString(), "[^a-zA-Z0-9]", "");

 

See I already formatted it on the datetime column in the gridview and this works fine displaying in the gridview:

gridViewTextBoxColumn16.DataType = typeof(System.DateTime);
gridViewTextBoxColumn16.FieldName = "approvalTime";
gridViewTextBoxColumn16.FormatString = "{0:yyyy/MM/dd HH:mm}";
gridViewTextBoxColumn16.HeaderText = "approvalTime";
gridViewTextBoxColumn16.IsVisible = false;
gridViewTextBoxColumn16.Name = "approvalTime";
gridViewTextBoxColumn16.ReadOnly = true;

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 31 Jan 2020, 09:47 AM
 

Hello, Lily,   

The column's FormatString property controls how the cell's Text will be formatted. The specified DataType of the column controls what values will be stored in the cells. Hence, the cell's Value property will return a DateTime value.

In order to get the correct format, feel free to use the DateTime.ToString method passing the specific format that you want. Additional information about DateTime formatting is available in the following MSDN article: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

I have prepared a sample code snippet for your reference:

            GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Col1");
            textColumn.DataType = typeof(DateTime);
            textColumn.FormatString = "{0:yyyy/MM/dd HH:mm}";
            this.radGridView1.Columns.Add(textColumn);
        private void radButton1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Col1 " + ((DateTime)radGridView1.CurrentRow.Cells["Col1"].Value).ToString("yyyy/MM/dd HH:mm"));      
        }
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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Lily
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or