Visual Basic .Net

with VB.Net: When using a DataViewGird,and entering text into a column and then tabbing or clicking another column and if the data entered is not what it should be, how do I give a message, and keep focus on that column.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
You can use the CellValidating event of the grid, and if the validation fails, set e.Cancel = true to stay in that cell
slay21455Author Commented:
Thank you for your answer, however

Sorry, but the answer does not give me enough detail.  I am not that familiar with Vb.Net and would like a more detailed example of the events and the code.

Éric MoreauSenior .Net ConsultantCommented:
here is a sample:

Option Strict On

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        DataGridView1.DataSource = GetSomeData()
    End Sub

    Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        If e.ColumnIndex = 1 Then
            If e.FormattedValue.ToString() <> "Valid" Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Function GetSomeData() As DataTable
        Dim dt As New DataTable("Test")
        With dt
            .Columns.Add("ID", GetType(Integer))
            .Columns.Add("Description", GetType(String))

            .Rows.Add(New Object() {1, "Valid"})
            .Rows.Add(New Object() {2, "Valid"})
            .Rows.Add(New Object() {3, "Valid"})
            .Rows.Add(New Object() {4, "Valid"})
            .Rows.Add(New Object() {5, "Valid"})
        End With
        Return dt
    End Function

End Clas

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

slay21455Author Commented:
Thank you very much Eric, I appreciate the answer and the quit response.
Éric MoreauSenior .Net ConsultantCommented:
you need to accept the comment if it fulfills your requirement to close the question
Éric MoreauSenior .Net ConsultantCommented:
Got good comments on that proposed solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.