?
Solved

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

Posted on 2011-10-25
8
Medium Priority
?
357 Views
Last Modified: 2012-08-13
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
Comment
Question by:Tim313
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37025265
>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
 

Author Comment

by:Tim313
ID: 37026427
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37027506
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Tim313
ID: 37031937
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37032278
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
 

Author Comment

by:Tim313
ID: 37032515
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
 
LVL 83

Expert Comment

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

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

839 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