Krypton-Suite / Standard-Toolkit

An update to Component factory's krypton toolkit to support .NET Framework 4.6.2 - 4.8.1 to .NET 8 - 9
BSD 3-Clause "New" or "Revised" License
408 stars 62 forks source link

[Bug]: KryptonDataGridView - Event CellEndEdit, CellValueChanged, ... Cell has always no "Value" #654

Open DarthVader85PA opened 2 years ago

DarthVader85PA commented 2 years ago

Describe the bug KryptonDataGridView-Cell has always null on Cell-Property Value. I have tested it with Windows-Control DataGridView successfully. I tested with Krypton-Versions: 5.550.2106.1, 6.2201.4, 60.22.2.32 (always the same issue).

To Reproduce VB.Net:

Private Sub dgvTest_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvTest.CellEndEdit
   'If Index < 0 then exit sub.
   If e.RowIndex < 0 Then Exit Sub
   If e.ColumnIndex < 0 Then Exit Sub

   'Value has always NULL/Nothing
   KryptonMessageBox.Show(dgvTest.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, "Test", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

C#:

private void dgvTest_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // If Index < 0 then exit sub.
    if (e.RowIndex < 0)
        return;
    if (e.ColumnIndex < 0)
        return;

    // Value has always NULL/Nothing
    KryptonMessageBox.Show(dgvTest.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Desktop (please complete the following information):

PWagner1 commented 2 years ago

Hi @DarthVader85PA

Does this affect all cell types?

DarthVader85PA commented 2 years ago

Hi @Wagnerp

That's a good question!? lol I tested with DataGridViewTextBoxColumn. A KryptonDataGridViewTextBoxColumn is available too?! Should I use this one?

PWagner1 commented 2 years ago

Hi @DarthVader85PA

Please try using KryptonDataGridViewTextBoxColumn, as these were designed to use Krypton

Smurf-IV commented 2 years ago

the following part of the statement means that the underlying datasource (However that is defined) does not have a value, and it has nothing to with the Krypton drawing part of the Windows DataGridView: dgvTest.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

The above can also be accessed via dgvTest[e.ColumnIndex, e.RowIndex).Value If these are strings, then they are allowed to be null ! Use the FormattedValue to see the representation on the screen. (Which can still be null!!)

giduac commented 2 years ago

Hi @DarthVader85PA,

Been having a look into this.

Krypton and standard datagrid column types have a settings to handle empty values entered in the grid. DataGridView.Columns[ N ].DefaultCellStyle.DataSourceNullValue

If this value is left to it's default a value of DBNULL will be assigned when the cell is empty. DataGridView.Columns[ N ].DefaultCellStyle.DataSourceNullValue = System.DBNull.Value

In the case of a text column. To prevent the error you can set it to: DataGridView.Columns[ N ].DefaultCellStyle.DataSourceNullValue = ""; //string.Empty

I tried with both Krypton and standard DataGridView. The first having 1 Krypton Tex tColumn and standard Text Column.

Both views and columns produce the null error when a cell is empty upon EndCellEdit. So it could be that your DataGridView has the above null handler set.

Since you can't enter an empty string for the DataSourceNullValue in the property editor. This has to be done from code when the program/datagrid is initialized.

Docs: https://docs.microsoft.com/en-us/dotnet/api/system.dbnull?view=net-6.0

EDIT March 7: Rephrased two lines and removed a typo.

giduac commented 2 years ago

Hi @DarthVader85PA ,

Did the above answered your question ?

DarthVader85PA commented 2 years ago

Hey @Smurf-IV, @giduac

Sorry that you have waited for my answer. The issue is not a default value for DBNull.

I get no value that the user have added/wrote when he leaves the cell. The value is allways empty?! DGV_NoValue_CellEndEdit

giduac commented 2 years ago

Hi @DarthVader85PA

I tried this with your Demo. Both Winforms columns & Krypton columns Unable to reproduce with both column types. CellEndEdit always has the cell value set and is readable.

What is the use of this statement since the obj is not used after assignment image

Can you prep the sample so it produces the error and post it again?

DarthVader85PA commented 2 years ago

Hi @giduac

It's crazy. Same code in a Demo-Project and it works. The difference only is: I fill DataTable manually, not with a SQL-DataAdapter?!

But: When I use KryptonDataGridViewComboBoxColumn instead of DataGridViewComboBoxColumn I get a type error?!

Here my little Project: CellValueDemo.zip

giduac commented 2 years ago

Hi @DarthVader85PA

Can you please post a video that clearly shows the error when triggered in the CellValueDemo test program?

DarthVader85PA commented 2 years ago

Hi @giduac

Here a video. I hope you mean that? DGV_KryptonComboBox_Error

giduac commented 2 years ago

Hi @DarthVader85PA ,

I added a standard DGView to the demo. It uses standard columns. It has the same DB connections / columns config as the KDGV. But has no event handlers connected.

The underlying table you are using does not accept null values. So when you do a lookup and it returns no result the row will get saved and that is not accepted by the table's configuration.

So before anything can be saved/written you have to verify that all of the input is correct and complete.

Please test a bit with the demo below 654-CellValueDemo-withStdDGView.zip

DarthVader85PA commented 2 years ago

Hi @giduac

Thanks for your demo. I will test it a little bit! :)

Back to the Format-Error: I think, the Combobox of KDGV will return the DisplayMember (String) to the cell which expect Int32 (ValueMember) instead of String. ValueMember is in the table "mitarbeiter" (Column "id") in my demo Int32 - same for column "mitarbeiter_id" in table "arbeitszeiten". When I use the Windows DataGridViewComboboxColumn, it works fine! ;)

The other error with the Cell-Value that is NULL in my project: I will analyse it why. The code is the same to the demo here?! Is it better to close this Issue and open a new Issue for the Combobox-Column?

giduac commented 2 years ago

@DarthVader85PA The standard combo box column also triggers a write when there's a change in the cell. When the lookup fails the id cannot be written because there isn't one.

So you have to cancel the edit when there's insufficient data and prevent it from being written to the table.

PWagner1 commented 2 years ago

@DarthVader85PA Have you had any more luck with it yet?

DarthVader85PA commented 2 years ago

Hi @Wagnerp, @giduac

No sorry. No luck with this. If I use DataGridViewComboBoxColumn so it works as expected. With KryptonDataGridViewComboBoxColumn it comes to the Data-Error. I believe the value of DisplayMember is returned. Not the value of ValueMember.

grafik

Please use the demo from @giduac and change in KryptonDataGridView the columns as the animated gif shows.

PWagner1 commented 2 years ago

Hi @DarthVader85PA

Thanks for the response, the odd thing is neither the DisplayMember or ValueMember is referenced anywhere?

DarthVader85PA commented 2 years ago

Hi @Wagnerp

Here are the properties for the binded column "Mitarbeiter". grafik

DataPropertyName: "mitarbeiter_id" is a column in DataTable "dtArbeitszeiten" which is binded to BindingSource "bsArbeitszeiten" and expected Int32-DataType. DataSource for Column "Mitarbeiter" is the BindingSource "bsMitarbeiter". ValueMember is a ID-Field and Int32-DataType in DataTable "dtMitarbeiter". The DataTables are created by Visual-Studio-Designer, is not strongly-typed; the name: "dsDaten".

Sorry for the German-Words. Arbeitszeiten = working hours Mitarbeiter = employees Daten = data

Smurf-IV commented 4 months ago

@giduac Can you revisit this and fix please ?

giduac commented 4 months ago

Hi @Smurf-IV,

@DarthVader85PA Does this error still exist for you?

Yes but that can take a bit before there's more time... Please assign to me, ty.

PWagner1 commented 4 months ago

Hi @Smurf-IV,

@DarthVader85PA Does this error still exist for you?

Yes but that can take a bit before there's more time... Please assign to me, ty.

Hi @giduac

Now assigned to you

DarthVader85PA commented 4 months ago

Hi @wagnerp, Hi @giduac, Hi @Smurf-IV,

Yes, the problem still exists. I have to admit, though, that I'm no longer using the latest version. I would have to create a new demo with the latest nightly version?!

Greetings from Germany (Notice: Translated with Google 😅)

giduac commented 1 month ago

KryptonDataGridView and it components have several shortcomings which we will try to address in the V100 release or earlier if possible.

giduac commented 1 week ago

Hi @DarthVader85PA,

The second part of this ticket is caused by the combobox column bug https://github.com/Krypton-Suite/Standard-Toolkit/issues/1241. Which has been solved in alpha. Will commence testing as soon as the next nightly is ready.