?
Solved

Required field validation In datagrid view Columns

Posted on 2009-03-31
5
Medium Priority
?
1,539 Views
Last Modified: 2012-05-06
i am working on windows application, using VB.Net. How can i Implement required field validation in dataGrdiview columns. The user should not be able to submit the form until he selected index is changed from the combobox or Something is enterd in the textbox...
0
Comment
Question by:yuvaratna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24031273
The CellValidating event with the e.Cancel = True forces the user to enter something
0
 

Author Comment

by:yuvaratna
ID: 24031333
How can i show a message box if the user hasnt entered anything....should i check the value to string.empty and then display the user a message that the textbox cant be empty????
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 24031380
ok, then you have to loop on each item in the datagridview

Like:

    Function ValidColumn() As Boolean
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
 
            If Not row.IsNewRow Then
 
                Dim dt As DateTime
                If row.Cells(3).Value IsNot DBNull.Value AndAlso Not Date.TryParse(row.Cells(3).Value, dt) Then
                    Return False
                End If
 
            End If
        Next
 
        Return True
    End Function
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ValidColumn() Then
            Stop ' Not valid
        End If
    End Sub

Open in new window

0
 

Author Comment

by:yuvaratna
ID: 24031484
Thanks a lot ipaulino..give me a minute...I will check if it works....
0
 
LVL 2

Expert Comment

by:minjiber
ID: 24037808
Assuming your validating the 3rd column of the datagridview, use the below code which uses the RowValidating event

Private Sub DataGridView1_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
        If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
            Dim c As DataGridViewCell = DataGridView1.Rows(e.RowIndex).Cells(2)
            If IsDBNull(c.Value) Or c.Value = "" Then
                c.ErrorText = "Cell cannot be empty" ' or msgbox("Cell cannot be empty") as u prefer
                e.Cancel = True
            Else
                c.ErrorText = ""
                e.Cancel = False
            End If
        End If
    End Sub

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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