Problem with DatagridView, associated dates and event

Closed
0
0

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.

  • You must to post comments
0
0

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.

 

  • Francesco Leo
    Thanks, but if I remember correctly you can’t change the column types if they are full, so I have to clone the datatable first, then change the column type and then import the data, or am I wrong?
  • Luca (ITG)
    There is no changing of a type of an object after it’s created. The code sets the Format string. To “change” the column type override CreateDataGridViewColumnFromType() and create the type you like or set the property you want.
  • You must to post comments
0
0

Thank you for the clarification. See below:

 

  1. The datatable viewer is entirely managed by Visual Studio and I can use it without issues. The DataTable as well is not a Wisej class.
  2. The DataGridView in WinForms has a bug/feature when you don’t specify a format. See screenshots. It mixes formats when a date is set with a midnight time and when it’s not. Wisej correctly shows the correct format for all cells.
  3. Use DefaultCellStyle.Format = “d” in WinForms to correct the problem in WinForms and in Wisej.NET to use the format your application requires.

HTH

 

  • Francesco Leo
    However, I see that the column type is not Datetime, but DataGridViewDateTimePickerCell, perhaps this is the real reason, in any case I will have to write an extension to correct all this.
  • Luca (ITG)
    DateTime is the data type. The column type in Winforms is DataGridViewTextBoxColumn. Wisej.NET has more column types and automatically creates DataGridViewDateTimePickerColumn.
  • You must to post comments
0
0

[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

  • Alaa (ITG)
    It’s because the value is actually the default .NET DateTime string representation, you’ll have to control it via formatting, you can even set it up on a column level so that you don’t need to handle the CellFormatting event, the DataGridViewDateTimePickerColumn has the format value that you can set to “d”.
  • You must to post comments
0
0

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

  • Francesco Leo
    Exactly how I use it in my WinForms application (which has been working for years) that I’m testing with Wisej: if (vricette.Rows[e.RowIndex].Cells[“ticket”].Value != DBNull.Value && vricette.Rows[e.RowIndex].Cells[“ticket”].Value.ToString() == “0”) { vricette.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.MediumSpringGreen; } else { vricette.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightSalmon;}
  • Alaa (ITG)
    Tried the same thing and it works for me, you’ll need to debug your code and see where your logic is failing.
  • Luca (ITG)
    BTW that’s the wrong way of using CellFormatting also in WinForms. You should not change the the DefaultCellSytle when processing the CellFormatting event, the args have a Style property that should be used to format the cell. In Wisej both work. This issue is closed, if you have another reproducible issue related to Wisej please open a new one and attach a small reproducible test case.
  • You must to post comments
0
0

[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)

Attachment
  • You must to post comments
0
0

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.

  • You must to post comments
Showing 6 results