Force cell format after setting value in ClosedXML - c#

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;#";

Related

C# DatagridView change the text entered in a cell during validation

i have a datagridView with a cell whose data type is Datetime.
The value of the cell is formatted as HH:mm (hours, minutes).
When the user edits the cell with a value like 5, I would like to transform it into 05:00 before the datagridView complains that the value entered is not a valid datetime.
I have tried the validating event but there I cannot change the Formatted value, I can only Cancel.
I know what I am asking is possible because I have done it in another project, but I cannot access the code of that project to see how I did it.
Please help. Thanks
I have solved the problem using the CellParsing event. There I get the Value entered from the user and I can parse it into a proper datetime and finally set e.ParsingApplied = true (very important otherwise the parsing you did is ignored)

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.

DateTime To Excel Region Issue

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";

Categories