DateTime To Excel Region Issue - c#

I am strongly typing my DataTable DateTime columns and then transforming the DataTable into an Object[,] and pasting into Excel. My problem is that in some cases the date shows in US format and in others UK format. What is the standard practise for doing this operation and making sure that the correct date is pasted down but also maintaining the type of the column?

With C# Excel Interop, you can set the Format of a cell or a range of cells with the NumberFormat property.
This is just a string, and is the same thing that you would type into Excel if you right clicked on a cell, or a range of cells and selected the Custom category, and then typed in a format.
You could ensure that a Date in a cell always has the same format by simply doing the below:
range.Value = DateTime.Now.ToOADate();
range.NumberFormat = "dd/mm/yyyy";

Related

Force cell format after setting value in ClosedXML

I have an interesting issue that I can't seem to figure out how to solve.
I need to export a date into excel in one format, but display it in another.
The user should see: ddMMMyyyy, but they enter it in MM/dd/yyyy. The problem I am having is that if I set the cell value to a MM/dd/yyyy format, it is not auto formatting to the ddMMMyyyy unless I manually edit the field in excel.
Is there any way to force the formatting?
worksheet.Cell("I" + rowIndex).SetValue<String>(LastUpdated.ToString("MM/dd/yyyy"));
worksheet.Cell("I" + rowIndex).Style.DateFormat.Format = "[$-409]dd-mmm-yyyy;#";

How to change Datetime format in a DataGridView cell

I am stucked about datagridview control in which i need to type datetime format in a cell like my own format i need.
I want from the client to enter datetime like "mm/dd" and hit enter, then this field will be seen as a short datetime format.
For example: Client will write "06/27" and hit enter. In cell, it will show 06/27/2015. (The year that we are in). Why i need is that, i will be able to send data in a format that in SQL server accepts.
Should i need also to use it with datetimepicker control or without it i can also handle it?
Well, if you really need to change the value of cell you can use dataGridView_CellEndEdit event and then access cell with:
var cell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];

Force string format in Excel cell

I'm currently exporting to Excel a RadGrid and I have some cells that have the text "1 / 10" meaning that they had 1 hour used for 10 units. When this gets to Excel, it thinks the cell is a date, so it changes the cell to be January-10 which is not what I'm wanting.
I've gone the other direction before (changing a string to a number/date) but I've never had the issue where I needed to make Excel keep the cell as a string and not try to change the format.
How do I do this? I've tried adding a apostrophe to the beginning of the cell text:
e.Cell.Text = String.Format("'{0}", e.Cell.Text);
but that seems to just make Excel display two apostrophe's at the start of the cell:
Update:
I'm using Telerik RadGrid (v2012.2.929.40) and all it does is generate some HTML for Excel to open (you always get that horrible prompt from Excel when trying to open it). So before, if I wanted something to be formatted as a number like I had specific in the grid, I could have the following event that would set the mso-number-format. But, this doesn't quite work when I want Excel to display the cell value as strictly text.
protected void RadGridQuote_ExcelExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
{
if (e.Cell.Style["mso-number-format"] != null)
{
e.Cell.Style["mso-number-format"] = "/#";
e.Cell.HorizontalAlign = HorizontalAlign.Left;
}
}
If you're using Excel Automation in C#, I've found the best results would be to set the NumberFormat first, which you should set to "#". Then set the value in your .Text property. (If you do it in reverse, it'll convert the date into a number, which you don't want.)
See also MSDN regarding NumberFormat.

Datatable and Date Column Manipulation

I am working on a database driven c# application.
I have a datatable that has a date column in it. Bind this datatable to datagridview and let the user edit the records and upon clicking save, save these changes in the database.
Date format in grid changes based on the system settings between dd/MM/yyyy and MM/dd/yyyy.
Data entry operators are not smart enough to look at this difference, so while editing the records they sometime move the records to different month.
Date Format is dd/MM/yyyy 10/10/2010
They want to change it to 12th October from 10th October. They made this change 10/12/2010 assuming the date format is MM/dd/yyyy.
There always remains this problem, so I want to fix the date format in the datatable.
What are the approaches I could use?
Use this code
var style = new DataGridViewCellStyle();
style.Format = "dd-MMM-yy";
colDate.DefaultCellStyle = style;
You don't need to convert it back and forth, it'll do all the things you want to achieve,
Are you setting the Column DataType as DateTime? I don't understand why this issue can occur. Internally, .NET should be saving this cell value as DateTime, and passing this to the database. Date formatting is not an issue.

DataGridView Format Decimal Values with AutoGenerateColumns

I have a datagridview that uses AutoGenerateColumns in winforms. There is a date column and then 1-16 data columns containing numeric values. I want all of these 1-16 data columns to be formatted to 4 decimal places.
As per this post, you set the DefaultCellStyle through the designer. I did this manually to get what format that I want to use. I set this in the form constructor:
this.dgv_PreviewGrid.DefaultCellStyle.Format = "N4";
I have tried setting it this way and also tried manually on each column on DataBindingComplete event. Nothing happens! The app lags as though it is doing the rounding, but doesn't.
Also, is there a link to msdn or something with a list of the DefaultCellStyle formats? Couldn't find them.
EDIT: I found that using Format "D4" instead of "N4" (Decimal vs. Number) --> is probably the format that I want, but it is still not working.
The MSDN page you want is Standard Numeric Format Strings.
You want the N or F format specifier, not D. D is for integers.
Setting grid.DefaultCellStyle.Format should work, but since you have a date column you will need to override that column's DefaultCellStyle, or the dates won't display.
What is the actual type of the numeric property on your datasource? Are you sure it isn't a string? How are you populating the DataGridView?
Without more information it's difficult to guess the problem. It might help if you show the code that populates the DataGridView.

Categories