Tim313
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?
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_User AddedRow(B yVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewRow EventArgs) Handles TblSched2DataGridView.User AddedRow
TblSched2DataGridView.Curr entRow.Cel ls(1).Sele cted = True
End Sub
Private Sub TblSched2DataGridView_Cell Validating (ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataG ridViewCel lValidatin gEventArgs ) Handles TblSched2DataGridView.Cell Validating
If TblSched2DataGridView.Curr entRow.Cel ls(1).Edit edFormatte dValue = 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.
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_User
TblSched2DataGridView.Curr
End Sub
Private Sub TblSched2DataGridView_Cell
If TblSched2DataGridView.Curr
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).IsNew Row Then
If IsValidValue(dgv.Rows(e.ro windex).ce lls(0).val ue) = False Then
dgv.CurrentCell = dgv.Rows(e.rowindex).cells (0)
End If
End If
dgv_RowEnter:
If dgv.Rows(e.RowIndex).IsNew
If IsValidValue(dgv.Rows(e.ro
dgv.CurrentCell = dgv.Rows(e.rowindex).cells
End If
End If
ASKER
CodeCruiser,
In your code, "IsValidValue" is not available to me. I made adjustment. I tried the following per your suggestion:
Private Sub TblSched2DataGridView_RowE nter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles TblSched2DataGridView.RowE nter
If TblSched2DataGridView.Rows (e.RowInde x).IsNewRo w Then
If TblSched2DataGridView.Rows (e.RowInde x).Cells(1 ).EditedFo rmattedVal ue = Nothing Then
TblSched2DataGridView.Curr entCell = TblSched2DataGridView.Rows (e.RowInde x).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.Curr entCell = TblSched2DataGridView.Rows (e.RowInde x).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
In your code, "IsValidValue" is not available to me. I made adjustment. I tried the following per your suggestion:
Private Sub TblSched2DataGridView_RowE
If TblSched2DataGridView.Rows
If TblSched2DataGridView.Rows
TblSched2DataGridView.Curr
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.Curr
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CodeCruiser, you live up to your "Genius" ranking...
I did the following per your suggestion:
Public Shared RowNew As Boolean
Private Sub TblSched2DataGridView_RowE nter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles TblSched2DataGridView.RowE nter
If TblSched2DataGridView.Rows (e.RowInde x).IsNewRo w Then
RowNew = True
End If
End Sub
Private Sub TblSched2DataGridView_Sele ctionChang ed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TblSched2DataGridView.Sele ctionChang ed
If RowNew = True Then
TblSched2DataGridView.Curr entCell = TblSched2DataGridView.Curr entRow.Cel ls(1)
RowNew = False
End If
End Sub
Works great, just what I needed!!! Thank you!
I did the following per your suggestion:
Public Shared RowNew As Boolean
Private Sub TblSched2DataGridView_RowE
If TblSched2DataGridView.Rows
RowNew = True
End If
End Sub
Private Sub TblSched2DataGridView_Sele
If RowNew = True Then
TblSched2DataGridView.Curr
RowNew = False
End If
End Sub
Works great, just what I needed!!! Thank you!
Glad to help :-)
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.