Link to home
Start Free TrialLog in
Avatar of Tim313
Tim313Flag for United States of America

asked on

DGV - New Row - Force user to make entry into first column.

After a user adds a new row to a dgv, the first column needs to have an entry before any other column can take an entry. How can I accomplish this?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

>After a user adds a new row to a dgv

How? You can add the row programmatically (on button click) and set the focus to the first cell and add code on its endedit/leave/validate event to make sure a valid value is entered.
Avatar of Tim313

ASKER

CodeCruiser, thanks for your response.

My dgv "AllowUser To AddRows" property is set to "True" which results in a new row always showing. This is done to eliminate the need for the user to click on a button. When the user "tabs" or "right arrows" off the last column of the current row, the first column of the added row is selected. I have the following code which seems to work ok for this scenerio as well as the button click:

Private Sub TblSched2DataGridView_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles TblSched2DataGridView.UserAddedRow

        TblSched2DataGridView.CurrentRow.Cells(1).Selected = True

    End Sub

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

        If TblSched2DataGridView.CurrentRow.Cells(1).EditedFormattedValue = Nothing Then
            MessageBox.Show("You must enter a Code first.", "MISSING DATA")
            e.Cancel = True
        End If

    End Sub

 The problem is that since a new row is always showing, the user could click or "down arrow" onto the new row, not on Cell(1), which results in not being able to select Cell(1), just shows the MessageBox if you try to select Cell(1).

How can I keep all columns (except for Cell(1)) of the new row from being selected? Can they be disabled until Cell(1) has been validated? Maybe you have another method I can use to accomplish what I need.
You can try handling the RowEnter event and using the index to find if the row is newrow then focus the first cell if it does not already contain a valid value

dgv_RowEnter:
If dgv.Rows(e.RowIndex).IsNewRow Then
   If IsValidValue(dgv.Rows(e.rowindex).cells(0).value) = False Then
      dgv.CurrentCell = dgv.Rows(e.rowindex).cells(0)
   End If
End If
Avatar of Tim313

ASKER

CodeCruiser,
In your code, "IsValidValue" is not available to me. I made adjustment. I tried the following per your suggestion:

    Private Sub TblSched2DataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblSched2DataGridView.RowEnter
        If TblSched2DataGridView.Rows(e.RowIndex).IsNewRow Then
            If TblSched2DataGridView.Rows(e.RowIndex).Cells(1).EditedFormattedValue = Nothing Then
                TblSched2DataGridView.CurrentCell = TblSched2DataGridView.Rows(e.RowIndex).Cells(1)
            End If
        End If
    End Sub

and received the following:

InvalidOperationException was unhandled
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
On line:
 TblSched2DataGridView.CurrentCell = TblSched2DataGridView.Rows(e.RowIndex).Cells(1)

I would have thought setting the focus to the first cell of a new row would be easy. Any other suggestions you may have would be appreciated.

Tim
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim313

ASKER

CodeCruiser, you live up to your "Genius" ranking...
I did the following per your suggestion:

    Public Shared RowNew As Boolean

    Private Sub TblSched2DataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TblSched2DataGridView.RowEnter
        If TblSched2DataGridView.Rows(e.RowIndex).IsNewRow Then
            RowNew = True
        End If
    End Sub

    Private Sub TblSched2DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TblSched2DataGridView.SelectionChanged
        If RowNew = True Then
            TblSched2DataGridView.CurrentCell = TblSched2DataGridView.CurrentRow.Cells(1)
            RowNew = False
        End If
    End Sub

Works great, just what I needed!!! Thank you!
Glad to help :-)