Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid cell validating 1

Status
Not open for further replies.

kimprogrammer

Programmer
Sep 15, 2008
160
CA
Hello
I have a datagrid and would like to validate one field. I've copied some code from someone elses thread and made some changes.

When I select data from my first combo box (not part of the datagrid) I get this error and shouldn't.

I am new to vb and am teaching myself so I'd appreciate help in understanding how cell validation works.

Here is my code:

Private Sub SwipeAccumulativeHoursDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles SwipeAccumulativeHoursDataGridView.CellValidating
If SwipeAccumulativeHoursDataGridView.Columns(e.ColumnIndex).DataPropertyName <> "" Then
Try 'check if numeric
SwipeAccumulativeHoursDataGridView.Columns(e.ColumnIndex).DataPropertyName = Decimal.Parse(SwipeAccumulativeHoursDataGridView.Columns(e.ColumnIndex).DataPropertyName)
Catch ex As Exception
e.Cancel = True
MessageBox.Show("Amount - Must enter a numeric field", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
SwipeAccumulativeHoursDataGridView.Columns(e.ColumnIndex).DataPropertyName = ""
End Try
End If
End Sub


Thanks
Kim
 
Sorry
I meant that the error is the error message that is in the message box. I should have been more clear.

MessageBox.Show("Amount - Must enter a numeric field", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
Could you explain the (e.ColumnIndex) and where I get that.
I think that could be the problem and that I'm not telling it the correct column.

I think this could be causing the problem with my other post today where I am not displaying the combobox.
 
You're covering up the actual error message with your own. While debugging, it is helpful to see the actual error message. You could temporarily code a Messagebox.Show(ex.Message), or set a break point to check out the error message.

Anyways, it looks like you are working with DataPropertyName, but it sounds like you really wanted to validate the value of the cell. If the value must be numeric, is it possible for you to set the DataType of the column of your data source to a numeric data type? If you do this, it will take care of the validation warnings for you. However,the validation message is ugly, and you can use the DataError event to put in your user friendly error message.
 
Kim, your event is firing for each cell. e.Index refers to the index of the current column which is being validated. You probably just want to test this on one of a few columns, so you need to examine e.Index and see if it refers to the column you need.
 
I just did a search for e.index and didn't find to much explaining it.

I only have one column that I want to check if it is numeric and a date column that I'd like to validate.

I changed the (e.ColumnIndex) to (e.Index) and recieved compile errors.

So I don't think I'm grasping this.
 
It was a typo on my part. It should be e.ColumnIndex.

Let me ask you this: is your numeric column defined as numeric in your database, and is your date/time column defined as datetime?

ADO.Net is kind of set up so that you don't need to worry about this stuff if your physical database design matches your logical design.
 
Kim,
Here is some advice which you can choose to accept or disregard, it's up to you. I'm assuming you're creating your strongly typed datasets and table adapters with the GUI and kind of creating a RAD database program. Until you're familiar with all of the intricacies of ADO.Net, you might want to pick up an ADO.Net book and practice creating your connections, datasets, dataadapters, commands, etc. in code. It makes a lot more sense when you can see and create what's going on behind the scenes. You can sometimes get away with less code and overhead when you create your own code as opposed to what the GUI creates for you as well.
 
In my database my 1st field is defined as decimal and the date is defined as datetime. (I do remember seeing a visual basic how do I video where you put the validation on the dataset instead of in the code.)
------------------------------------------
I looked at amazon for ado.net books; is there one you suggest?
I do have a visual basic 2005 - wrox book and 2 programing in visual basic books by Bradley and Millspaugh which I refer to alot.
How would the ado.net book be differnt from them?
----------------------------------------------------
But just a little info on my situation. I was hired by a company as and analyst programmer. They told me they were more intested in my analyst skills and I could learn the programming when I started. I had no idea at the time that the IT manager has absolutly no IT experience. I was told I'd be developing in VB6, and there was a person here how knew it, so for my first month here I was teaching myself vb6, until I found out they don't have it, it's ,net. So I have to catch up (my deadline didn't change and I also needed to learn Crystal reports for my reports, and had framework security issues that used up alot of time).

So this has been an extremely intense learning curve because I've been a cobol/structured programming programmer. And even the whole vocabulary for me has changed.

I only have 1 month left to show some results because they do not understand the learning curve for this (I mentioned an IT manager with no knowledge of this - who can't understand why this isn't as fast and easy as using excel)

So I'm not sure getting the book to understand this will help me in my timeframe at the moment. Although I would like to understand this better than I do.
 
OK, let's take this step by step. Your DataSet already has the correct types defined if they are defined correctly in your database--which they are.

Do me a favor, comment out all of the code in your SwipeAccumulativeHoursDataGridView_CellValidating routine, and try to add a word into your decimal field of your DataGridView, and navigate off the record. You should get a nasty looking error message on your screen. It's already done the validation for you. However, it's ugly, so you want to give the user a friendly message.

Now, open the DataError event for your DataGridView. You are interested in displaying a friendly error message for your first and second columns. .Net arrays are zero based, so this would be Columns 0 & 1. Put this code into your DataError event handler.
Code:
        Select Case e.ColumnIndex
            Case 0 'Needs to be a decimal
                MessageBox.Show("You must enter a valid decimal.")
            Case 1 'Needs to be a datetime
                MessageBox.Show("You must enter a valid datetime.")
        End Select
This is basically saying that you've got a data entry error, and depending on the column with the error, you want to display a custom message.

As far as books go, Wrox books are usually pretty good. If it has a detailed ADO.Net section, that would suffice.

Good luck on your project. Just remember--don't get too caught up in the syntax at first. It sometimes helps to take a step back and look at the big picture of what you're trying to accomplish. .Net can be hard to work with sometimes because it has so many libraries. Eventually, it will all come together, especially once you figure out which objects are subclassed from others. There's a lot of inheritance in .Net.
 
Ok my code is commented out in the cell validation.
But I can't seem to add a record in the data grid.
I have data in the datagrid is has been loaded from the file.

if I go to the blank line I can't enter anything. I also tried selecting the add button in the navigator that was automatically created. It creates a new blank line and I still can't add anything.

But if I tab and select enter on one of the existing lines I do get the datagridview default error dialog with all the messy info you talked of.

In the designer where I dragged the dataset to create the datagrid; the box that says datagridview tasks. I have "Enable adding" checked and everything else unchecked. Could I have something not set properly in the Edit Columns?
 
You might have ReadOnly set to true on the DataGridView. Either way, you should be able to test the code I gave you on either a new record or editing one which already exists.
 
That error is actually being cause by the blank line i added and not the existing record.
 
Does that e.columnindex refer to the index of the column as it appears in the datagrid on the screen or the whole datagrid? Because I am not showing the whole file on the datagrid.
 
Don't worry about the blank line for now. Just try to change one of your values to something invalid.

And it applies to all of the columns in your grid.
 
In the Edit Columns all 4 of my columns are set to read only being false.
 
ok the readonly was incorrect and set to true.

I see how this works now - and I can put the validation inside the case statements..

Thank You so much for all your help today. I know you are here to help guide people to their answers but today turned into a whole lesson. I would give you a hundred stars for your patience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top