Setting the currentcell to the cell with an error

Hello,

I have just recently started using Visual Basic 2005 and I am currently having problems with my program. The program I am working on retrieves a table from the combobox (there are 4 tables) and displays the data in a datagridview. The table is then bound to textboxes, which will allow the user to enter or adjust data.

My problem is when the user enters a null value in a cell I want the program to catch the null cell and alert the user, then focus back to the cell for editing. The SQL database doesnt allow null values in the columns, but I have had trouble trying to catch the null values. It doesnt throw an error if the cells are left empty or " ", so here it what I did;

Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave

' catches a null value in the currentcell
If Me.DataGridView1.CurrentCell.Value.ToString = "" Then
' lets me know there is a null value
MessageBox.Show("NULL VALUE")
' rejects the changes
Me.BanksDataSet.RejectChanges()
Me.PaperDataSet.RejectChanges()
Me.QualityDataSet.RejectChanges()
Me.LEISDataSet3.RejectChanges()
End If

End Sub

What I want to do is have the program catch a null value, then focus on the cell that had the null value. I have tried many attempts with datagridview.currentcell(), but have been unsucessful.

Any help would be greatly appreciated. Thanks.

Ryan





Answer this question

Setting the currentcell to the cell with an error

  • JoshKorn

    Hmm, I will try that. Thank you for replying.

    Ryan

  • abh_pat

    Still did not solve the problem, does the same thing as before.

  • Jim Cordwell

    Try this

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim strConn As String = _
    "Server = .;Database = NorthWind;" & _
    "Integrated Security = SSPI;"
    Dim conn As New SqlClient.SqlConnection(strConn)
    Dim dt As New DataTable
    Dim da1 As New SqlClient.SqlDataAdapter _
    ("Select * from Orders", conn)
    da1.Fill(dt)
    DataGridView1.DataSource = dt
    End Sub

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

    If e.FormattedValue.ToString.Length < 1 Then
    MessageBox.Show("Enter a value")
    e.Cancel = True
    End If
    End Sub
    End Class



  • Boerboon

    Use the datagridview's cellvalidating event instead.


  • Setting the currentcell to the cell with an error