I'm working with a Syncfusion SFDatagrid in WPF and I want to show date on the string format.
var col = new CustomGridTextColumn();
col.HeaderText = ReportSourceDataTable.Columns[i].ToString();
col.DisplayBinding = new Binding(ReportSourceDataTable.Columns[i].ToString());
if (ReportSourceDataTable.Columns[i].DataType == typeof(DateTime))
{
col.DisplayBinding.StringFormat = "MM/dd/yyyy";
}
In the main application, it works fine.
But when I preview or export those data, then an unnecessary 'MM/dd/yyyy' string format is applied. Below are examples from a print preview
Can anyone tell me how I can remove this unnecessary string format?
You can achieve your requirement to displaying the correct date format in print preview by setting the UseBindingValue in GridTextColumn like below,
<syncfusion:GridTextColumn MappingName="OrderDate" UseBindingValue="True" DisplayBinding="{Binding Path=OrderDate,StringFormat=MM/dd/yyyy}"/>
Regards,
Jai
Related
I am having a problem while converting text into the format [hh:mm:ss].
When I double click, formatting is fixed and cell value is automatically aligned to the right.
I am using [SpreadsheetStreams.net] NuGet package to create an excel file.
Here what I have already tried:
1.
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()))
2.
Style test = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
test.NumberFormat = format;
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()), test) OR
writer.AddCell(columnNames[i].ToString(), test)
Please refer to this image:
How can I format the cell to [hh:mm:ss]?
After trying many solutions I found a way :
Create a Style object having a custom type like below :
Style timeStyle = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
timeStyle.NumberFormat = format;
Then pass this as the second parameter and first parameter as a day (in double)
writer.AddCell(TimeSpan.Parse(columnNames[i]).TotalSeconds / 86400, timeStyle );
I am trying to set a text box (which is type = "Date") to a string value which I have taken in from a database.
I have tried the following:
txtDOB.Text = tempRow["DOB"].ToString();
txtDOB.Attributes = tempRow["DOB"].ToString();
But unfortunately I am having no luck with it.
Thanks in advance.
First of all, make sure you have a valid value in tempRow["DOB"].
var dob = Convert.ToDateTime(tempRow["DOB"]);
After that, you can simply:
txtDOB.Text = dob.ToString("MM/dd/yyyy");
txtDOB.Attributes = dob.ToString("MM/dd/yyyy");
Typically this is due to the textbox not understanding the string format of the data you are providing it.
Try changing your code to this:
txtDOB.Text = tempRow["DOB"].ToString("yyyy-MM-dd");
txtDOB.Attributes = tempRow["DOB"].ToString("yyyy-MM-dd");
Validate when debugging (by using the Immediate window of Visual Studio) to see if "tempRow["DOB"].ToString("yyyy-MM-dd")" spits out a valid date string.
We have a process to return results of a search, and export the data to Excel. It's converting the results to a data table, and adding the data table to a blank worksheet:
var wb = new XLWorkbook();
wb.Worksheets.Add(ds);
return wb;
The users want some formatting added to the sheet - setting date fields to display as MM/DD/YYYY, for example.
Can't find the right reference to get to the worksheet in the new workbook, however.
Also unsure if one can set a format to a column, as opposed to the code below which I think is converting it to a string,
((Excel.Range)sheet.Cells[i, 2]).EntireColumn.NumberFormat = "yyyy-MM-dd";
sheet.Cells[i, 2].Value2 = Convert.ToDateTime(dr["date"].ToString());
Anyone?
EDIT - #ScottHannen identified the library I should be investigating, and I've made more progress. I have drilled into the worksheets and successfully added the formatting, but it's not reflecting as such in the final document.
Here's the code:
var wb = new XLWorkbook();
wb.Worksheets.Add(ds);
foreach (IXLWorksheet ws in wb.Worksheets)
{
if (value == 1)
{
ws.Column(16).Style.DateFormat.Format = "yyyy-MM-dd";
ws.Column(17).Style.DateFormat.Format = "yyyy-MM-dd";
}
else
{ }
}
return wb;
When I look at the final document, the dates are still displaying as verbose - 12/7/2017 12:00:00 AM - but the formatting IS in the document. If I do the manual "hit enter in the cell and exit" thing, the formatting updates, as does doing Text to Columns.
Of course, I don't WANT to do that, I want the formatting to take straight away.
I assume the issue is that I'm adding the data first and formatting it after, just like a regular sheet. But is there a command in there to refresh/apply/what have you the formatting?
During run time I want to insert some integer value on textbox it should be display like currency format
example : 1000000 is input
Expect 1,000,000.00
The below code is execute failure format
example 12345678
but actually i got 85,671,234 after first four(1234)integer the cursor automatically moved to backside
The code is
txtpurchasecost.Text = string.Format("{0:#,###,###.##}", double.Parse(txtpurchasecost.Text));
Thanks in Advance
txtpurchasecost.Text = string.Format("{0:N2}", double.Parse(txtpurchasecost.Text));
and you may also specify the culture manually for the separators:
txtpurchasecost.Text = string.Format(
CultureInfo.CreateSpecificCulture("en-US"),
"{0:N2}",
double.Parse(txtpurchasecost.Text));
Also you can check this article: Standard Numeric Format Strings
int temp = 0;
if (int.TryParse(txtpurchasecost.Text, out temp))
{
txtpurchasecost.Text = (int.Parse(txtpurchasecost.Text)).ToString("C2");
}
try this, this will fix your issue.
I am building a winform based Desktop application. As part of the application, I am generating the Excel output.
I am getting a error, when I am trying to enter the date value.
In C#, the date value is "11-10-12". However, in Excel, it is printing as 10-11-2012.
Here is the code, which does it :
String Date1 = "11-10-12";
oSheet.Cells[i + 2, 1] = DateTime.ParseExact(Date1, "dd-MM-yy", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy");
Any idea what could could be wrong ?
EDIT
The Cell format in Excel was by default General. However, when the values are entered, it is changed to Date.
Try changing the format of the excel-Cell with:
yourCellRange.NumberFormat = "dd-MM-yy";
or adjust the string format to the excel-format.
Try doing this
worksheet.get_Range(Cell1, Cell2 + worksheet.Rows.Count).NumberFormat = "dd-mmm-yyyy";
When you are adding the value you can try
worksheet.Cells[row, column] = String.Format("{0:dd-MMM-yyyy}", Date1);
None of the suggestion helped unfortunately. So, I disabled the automatic formatting in Excel :
So, here is the code snippet which disables the automatic (Date) formatting in Excel :
String Date1 = "11-10-12";
oSheet.Cells[i + 2, 1] = "'" + Date1;
So, the trick is to preceed the String with an apostrophe which worked.
Thanks
Try just:
DateTime.Now.ToString("dd-MM-yy");
or:
String Date1 = "11-10-12";
var Format = "dd-MM-yy";
var TimeBuff = DateTime.ParseExact(Date1, Format,
CultureInfo.InvariantCulture).ToString(Format);
oSheet.Cells[i + 2, 1] = TimeBuff;
Both Parse and ToString must contains the same Format. If you get the same its definitely a Excel failure.
Edit:
Just for DJ KRAZE