• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

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?
0
Tim313
Asked:
Tim313
  • 5
  • 3
1 Solution
 
CodeCruiserCommented:
>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.
0
 
Tim313Author Commented:
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.
0
 
CodeCruiserCommented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Tim313Author Commented:
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
0
 
CodeCruiserCommented:
IsValidValue was just a place holder for your actual validation code.

I think the problem is that code may go into an infinite loop. Try using a boolean variable and set it to true in above code and then change cell in selection changed or other similar event.
0
 
Tim313Author Commented:
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!
0
 
CodeCruiserCommented:
Glad to help :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now