Answered DataGridView Validating (2)

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
Here's a DataGridView routine slashed to the very basics for simplicity. My question is:
How can I get the grid to display an altered value for a cell from within the cell's validating event-handling sub ? This mini-app is supposed to receive a date for input. If the user's input date does not fall on a Wednesday or a Saturday, the input value is adjusted to the following Wednesday or Saturday (which ever is closer). The adjusted date does not display correctly until after the second time the cell is visited. If you run this code, you'll see what I mean if you enter a date that does not fall on a Wednesday or a Saturday.

I'm trying to avoid using the cell's formatting event because it fires multiple times, even with just a "mouse-over", and I have the same display problems with it anyway...
VB.NET:
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents DGV As New DataGridView
    Private WithEvents addNewRowButton As New Button

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Size = New Size(350, 300)
        DGV.Location = New Point(60, 60)

        With addNewRowButton
            .Text = "Add Row"
            .Location = New Point(10, 10)
        End With

        Me.Controls.Add(DGV)
        DGV.ColumnCount = 2
        Me.Controls.Add(addNewRowButton)
    End Sub

    Private Sub DGV_CellValidating(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DGV.CellValidating

        If DGV.CurrentCell.IsInEditMode Then
            Dim dt1 As Date
            Dim dt2 As Date

            Try
                dt1 = CType(DGV.CurrentCell.EditedFormattedValue.ToString, Date)
                dt2 = CheckDayofWeek(dt1)

                [COLOR="Sienna"]'   >>>>  The following line sets the altered value into the Grid data (somewhere ???)
                '   but it does not display until the cell is revisited and exited again.[/COLOR]
                If dt1 <> dt2 Then DGV.CurrentCell.Value = dt2.ToShortDateString

            Catch ex As Exception
                MessageBox.Show("Invalid Date !")
            End Try
        End If
    End Sub

    Public Function CheckDayofWeek(ByRef dt As Date) As Date
        '   Returns the following Wednesday or Saturday from dt (which ever is closer).

        Dim n As Byte
        n = CByte(dt.DayOfWeek)

        If n <> 3 And n <> 6 Then
            MessageBox.Show("Date selection will be advanced to the following Wednesday or Saturday")

            If n < 3 Then
                n = CByte(3 - n)
            ElseIf n < 6 Then
                n = CByte(6 - n)
            Else
                n = 4
            End If

            Return dt.AddDays(n)
        End If
    End Function

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles addNewRowButton.Click

        DGV.Rows.Add()
    End Sub
End Class

???? :eek:
 
Last edited:
I havent tried it, but does calling the Refresh method work?

VB.NET:
If dt1 <> dt2 Then
  DGV.CurrentCell.Value = dt2.ToShortDateString
  DGV.Refresh
End if
 
Well, InertiaM... You pointed me in the right direction...(I think)...!
I had tried DGV.Refresh before, without success. But I looked at it again after your post and discovered an Intellisense option that I didn't see before. The "RefreshEdit" method seems to solve the problem. 'Don't know why I didn't see this before! I've been screwing with this for two days now. Thanks!
 
Back
Top