I’m continuing to port a WinForms app. In a DatagridView I associate a datatable, the result of a query. In my App the date columns are seen correctly (08/11/2023), in Wisej it shows them as datetime (08/11/2023 00:00:00). Also it’s impossible to open the datatable while debugging because I get an error: unable to load custom viewer, and it’s very annoying. Furthermore, the CellFormatting event doesn’t work.
How can I solve both problems?
Thank you.
Use “d” as the format, see https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
The “custom viewer” error maybe a problem with Visual Studio. I don’t know what it means to open a datatable while debugging.
[The “custom viewer” error maybe a problem with Visual Studio. I don’t know what it means to open a datatable while debugging.]
It’s not a Visual Studio error, because it only happens with Wisej.
To reproduce the error just create a Datatable (here I do it with a query select)
DataTable oSelect = Leo.mytable(“select * from customer”);
If you block the execution and try to see the contents of the Datatable, as you see in the attachment, you receive the error (I translated it from Italian)
Hi Francesco,
I can assure you that the CellFormatting event is indeed working, how exactly are you using it?
This is an example on how you should use it:
private void Dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Value = DateTime.Now.ToString("d");
}
}
Best,
Alaa
[Use “d” as the format, see https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings]
I’ve read the date/time formats, but I don’t understand, should I create the DataGridView from code? In WinForms and also in Asp.net the DATE columns are displayed correctly to me, not as DATETIME, why doesn’t this happen with Wisej? If I have to redo all the DataGridView logic it would take too long.
Thank you
Thank you for the clarification. See below:
HTH
Quick update, the reason the formatting is weird in winforms is because it creates a text column without a format and the default System.ComponentModel.DateTimeConverter strips the time when it’s midnight. Wisej.NET creates a DataGridViewDateTimePickerColumn (it doesn’t exist in WinForms) with a default format of “g”. You can customize it in several ways:
One is to handle ColumnAdded and change the format
private void DataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e) { if (e.Column is DataGridViewDateTimePickerColumn dateColumn) dateColumn.Format = DateTimePickerFormat.Short; }
Another is to override DataGridView.CreateDataGridViewColumnFromType() and create or customize any column.