Solved

cell validating in datagridview

Posted on 2006-06-13
5
735 Views
Last Modified: 2012-05-05
I am struggling to validate cells in datagridview view.
I have created a very simple vb application in vb studio 2005.
created datasourse, databinding etc. Dragged and dropped a datagridview on my form including the little toolstrip bar with
with add new row, delete and save buttons.

When I click add new row it creates a new row which is empty. If I fill in the two columns and click save it works.
If I forget to input a value it obviously gives me a vb empyt string error. Can you please show me a small example of how a validation would work. I tried the one from MSDE but could not get it to work.  Here is all the code I have in my application>

**********************************************************************
Public Class Form1

    Private Sub PupilRecordsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PupilRecordsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PupilRecordsBindingSource.EndEdit()
        Me.PupilRecordsTableAdapter.Update(Me.SchoolDataSet.pupilRecords)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'SchoolDataSet.pupilRecords' table. You can move, or remove it, as needed.
        Me.PupilRecordsTableAdapter.Fill(Me.SchoolDataSet.pupilRecords)

    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click

    End Sub

    Private Sub PupilRecordsDataGridView_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PupilRecordsDataGridView.CellEndEdit
        PupilRecordsDataGridView.Rows(e.RowIndex).ErrorText = String.Empty
    End Sub

    Private Sub PupilRecordsDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles PupilRecordsDataGridView.CellValidating
        If PupilRecordsDataGridView.Columns(e.ColumnIndex).Name = "Name" Then

            If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
                PupilRecordsDataGridView.Rows(e.RowIndex).ErrorText = "Empty"
                e.Cancel = True
            End If
        End If

    End Sub

End Class
************************************************************
I might have the code in the wrong place because nothing happens with that.

thanks

Max
0
Comment
Question by:maxgall
[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
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
vb_jonas earned 500 total points
ID: 16899930
Perhaps you should check the name here:

        If PupilRecordsDataGridView.Columns(e.ColumnIndex).Name = "Name"

it might be DataGridViewTextBoxColumn2 or something. If you change to:

        If InventoryDataGridView.Columns(e.ColumnIndex).DataPropertyName = "Name" Then

it will probably work since I guess "Name" is the name of your datacolumn.
0
 

Author Comment

by:maxgall
ID: 16913897
Yes, silly mistake I made there  thanks vm
0
 

Author Comment

by:maxgall
ID: 16932582
Thanks vm I can now validate the cells but the problem I have is when you add entry button a new row is created with a new ID Number generated. When clicking the add new entry button again a new row is created and the previous row not validated and when clicking on the save button a generic error about empty fields is created. Any Idea how I can prevent a new row beeing created when the previous one is still empty?

0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16932627
Interesting, shouldn't be so difficult, but I cannot think of something at the moment, and Im quite busy, so I would suggest you to add a new question, so someone else can have a look?
0
 

Author Comment

by:maxgall
ID: 16932707
Ok Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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