Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DGV Validation Error When Bindingsource is Sorted

Posted on 2010-11-16
6
Medium Priority
?
445 Views
Last Modified: 2012-06-22
I’m having a problem with a datagridview that is bound to an Access table.  If the bindingsource is sorted, the wrong dgv value is validated – this occurs randomly and I cannot force the failure.  It appears that, after I enter a new cell value and prior to validation, the dgv is re-sorted and the cell value immediately above the cell I just changed is used for the validation.

If a disable sorting, I do not have any problems with validating the cell.

Below is the code I use to bind the table to the dgv and my cell validation routine.

'**********************************************************

    Dim ds_Dataset As New DataSet("SE_Dataset")
    Dim dt_Names As DataTable = ds_Dataset.Tables.Add(Me.Name)
    Dim da_Names As OleDbDataAdapter = New OleDbDataAdapter
    Dim bs_Names As New BindingSource

'**********************************************************

    Private Sub Table_Bind()

        Dim str_SQL As String = "SELECT * " & _
                                "FROM tbl_SE_DS_Path_Names"
        cmd = New OleDb.OleDbCommand(str_SQL, con_SE_MDB)
        da_Names = New OleDbDataAdapter(cmd)
        Dim commandBuilder As New OleDbCommandBuilder(da_Names)
        da_Names.Fill(dt_Names)
        bs_Names.DataSource = dt_Names
        bs_Names.Sort = "Path_Name"

    End Sub

'**********************************************************

Private Sub dgv_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating

    If dgv.IsCurrentCellDirty Then
        dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
        If Not Validate_Path_Name(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
            e.Cancel = True
        Else
            bs_Names.EndEdit()
            da_Names.Update(CType(bs_Names.DataSource, DataTable))
        End If

    End Sub

'**********************************************************

Here is the normal sequence of events with the validation works correctly; ‘zz’ was the original cell value; ‘aa’ is the new cell value:

CellBeginEdit:         Row = 2   Value = zz
CellLeave:             Row = 2   Value = zz
CellValidating:        Row = 2   Value = aa

This is the sequence of events when a validation error occurs.  ‘zz’ was the original cell value, ‘aa’ is the new cell value, ‘www’ was the value in the row immediately above ‘zz’ prior to entering ‘aa’.  Note that I am getting 2 CellLeave and 2 CellEnter events when a validation error is going to occur.

CellBeginEdit:         Row = 2   Value = zz
CellLeave:             Row = 2   Value = zz
CellLeave:             Row = 2   Value = www
CellEnter:             Row = 2   Value = www
CellEnter:             Row = 0   Value = aa
CellValidating:        Row = 2   Value = www

I repeatedly changed the same values in the dgv until a validation error occurred.  I cannot force the error – it’s occurs randomly.

This problem DOES NOT OCCUR if I do not sort bs_names.  I’ve tried sorting the dgv, but the problem still occurs.

Hopefully, I’ve made some sense of what I’m experiencing.  When everything is working correctly, my Access table is updated on each cell change; on the random validation error, the table is not updated.

Any thoughts as to why sorting bs_names (or the dgv) appears to cause random validation errors?

Thank you in advance for your help!

George
0
Comment
Question by:CIW_George
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150322
Try moving this line

dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)

to the else part(the part which executes if value is valid)
0
 

Author Comment

by:CIW_George
ID: 34150376
If you do that, we're still working with the original cell value when doing the validation.  The CommitEdit is needed before the validation.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 34150619
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CIW_George
ID: 34150847
Preliminary testing seems to support your suggestion about the FormattedValue.  I'll pound on it for a while longer to confirm and let you know the results.

0
 

Author Comment

by:CIW_George
ID: 34155895
Thank you for your help CodeCruiser!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158145
Glad to help :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question