[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error Handling in DatagridView

Posted on 2009-04-24
12
Medium Priority
?
819 Views
Last Modified: 2013-11-08
I have a Datagridview that is bound to a dataset.

The datagridview contains four columns:

1. Dropdownlist of Companies
2. Dropdownlist of Accounts
3. Textbox: Debit Amount
4. Textbox: Credit Amount

The dataset does not allow null values in the 1,2 column.

In the datasets Partial Class I've added Error Handling for this and they show up in the Datagridview as they should.

However, the user is allowed to leave the row even though there are errors on the rows columns. When this happens, the DataGridViews DataError event is thrown and the users row is deleted. How to avoid this? Is there a post row change event that can check for errors?

Best Regards

Tomas
0
Comment
Question by:ekenman
  • 6
  • 4
  • 2
12 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24231582
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24235052
What error message do you have ? You can handle the error on DataError event and don't show it if isn't really an error.
0
 

Author Comment

by:ekenman
ID: 24248442
I was off yesterday, so sorry for the late answer.
CodeCruiser, I tried the RowValidating event, but it is thrown after the dataerror, so the row is allready gone.
The error message that I'm having trouble with is if the user omitted to select a company (column 1) , or account (column 2) from the dropdownlists. So what I really want is to force the user to fill these out before continuing. But... the row is deleted.
 
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ekenman
ID: 24248583
(And I've put the validation code in the dataset. )
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 24248616
Humm, but that way RowValidating  should work (like CodeCruiser suggested).
Try this way and you can use e.Cancel = True to force the user to fill the combo's:

    Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
        If (Me.DataGridView1(0, e.RowIndex).Value Is Nothing Or Me.DataGridView1(1, e.RowIndex).Value Is Nothing) _
            AndAlso Not Me.DataGridView1.Rows(e.RowIndex).IsNewRow Then
            MessageBox.Show("You must fill the comboboxes")
 
            e.Cancel = True
 
        End If
    End Sub

Open in new window

0
 

Author Comment

by:ekenman
ID: 24248662
I get Cancel is not a member of System.Windows.Forms.DataGridViewCellEventArgs
0
 

Author Comment

by:ekenman
ID: 24248681
But there is one in the dataerror event. OK, That works perfectly. THANK YOU!
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24248683
Can you show what you have and also what version of VB do you have ?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24248692
Ok, glad I could help!
0
 

Author Comment

by:ekenman
ID: 24248711
Sure did! Thanks!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24249531
I think i provided the original direction of using the RowValidating event!!!!
0
 

Author Comment

by:ekenman
ID: 24249756
CodeCruiser, the solution was not to use the RowValidating event since it does not work with error event handling in the dataset. The code that jpaulino provided should be in the DataError event since the DataGridViewCellEventArgs does not have a cancel method as the DataGridViewDataErrorEventArgs does. So it was a typing error by jpaulino. However, that was shat solved my problem.
I'm still gratefull for your effort in helping me.
/Tomas
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 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