Visual Basic .Net

slay21455
slay21455 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
You can use the CellValidating event of the grid, and if the validation fails, set e.Cancel = true to stay in that cell

Author

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.

Jack
Senior .Net Consultant
Top Expert 2016
Commented:
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

s
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
Thank you very much Eric, I appreciate the answer and the quit response.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
you need to accept the comment if it fulfills your requirement to close the question
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
Got good comments on that proposed solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial