0
votes

I know I need to provide some code, but I'm not sure what I should show, so please suggest if you can.

I have a bound datagridview on a Windows form. After the form loads and the datagridview gains focus (on mouse click), the first row (and a specific column) loses it's data, changing the cell's state to dirty. It doesn't matter where I click to bring the dgv into focus, that row/column always goes blank. What event is firing that may trigger that loss of data?

Again, any suggestions as to what code to post would be great. I know that will help answer this question.

Edit #1 This code is an infinite loop, but I'm adding it in response to a comment:

 Private Sub dgQCOrders_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgQCOrders.CellPainting
    If e.RowIndex = 0 And e.ColumnIndex = 9 Then
        If e.FormattedValue <> e.Value Then
            MsgBox("Changed")
        Else
            MsgBox("Unchanged")
        End If
    End If
End Sub

Edit #2:

Private Sub dgQCOrders_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgQCOrders.CellPainting
    If e.RowIndex = 0 And e.ColumnIndex = 9 Then
        If e.FormattedValue <> e.Value Then
            Me.txtTest.Text = "Changed"
        Else
            Me.txtTest.Text = "Unchanged"
        End If
    End If
End Sub

This test tells me that the new value is null, it is deleting that first record (which I already knew)--still don't know how to fix it!

Edit #3 More explanation:

Currently, the only event I'm handling is form_Load, which fills the dgv using the tableadapter for my (bound) dataset. I then bind the dgv to the binding source.

I know that this error only occurs when the dgv gains focus (I tested this by setting the focus to the dgv when the form loads). I have a series of checkboxes/listboxes/textboxes on this form as well that allows the user to filter the dgv dynamically (back-end, I filter the binding source). If I filter the dgv first, the same row and the same column (their indexes do not change) maintains it's value when I move the focus to the dgv. When I clear the filter, the same row and the same column, loses it's data again.

I did have the _CellStateChanged event firing after a user makes an edit. Currently, it is commented out so the data loss isn't reflected in my dataset.

Additionally, I have another dgv on a different form, bound the exact same way, with the _CellStateChanged event and everything fires and saves correctly. I have gone through the designer coding for both forms, I can't find any setting difference between the two.

I'm losing my mind over here! Any help is GREATLY appreciated!

2
Probably the OnMouseClick would be a good start. - thetimmer
@thetimmer Could you elaborate? Right now, I have the dgv saving every time a cell state changes. But that first click, to gain focus on the dgv, is the only time I lose data and only in that specific cell. Not sure how the mouseclick event could change that? - alybaba726
What is the column data type ? - Bioukh
@Bioukh the datatype from my datasource is a float, it is stored as a textbox column in my dgv - alybaba726
You can try to add a breakpoint in the CurrentCellDirtyStateChanged event to see what happens. - Bioukh

2 Answers

0
votes

I decided to recreate the form from scratch and this error is no longer occuring. I have compared the two sets of code and can't find one discrepancy between them. If anyone has this problem in the future, save time and recreate all of the objects related to your dgv.

0
votes

Maybe I understand that statement within the focus cell is not saved in the database table if U save

this problem I solved add blank textbox control to your form which contain dgv and named txtFocus placed it behand dgv and use its properties send to back Or Evoked by the bottom of the screen so do not show it then before save white then : txtFocus.Focus() sendKeys.send("{F2}")

only in this case U can save the data inside last cell changed in dgv best Reg Ashraf