Home / gaydatinguk com / Datagridview cellvalidating date

Datagridview cellvalidating date

I have a Data Grid View with many records, and each record has several date fields.The user is allowed to edit various fields on the datagridview, and I am trying to validiate the date fields before trying to update the database.I have written this: Private Sub dgv Top_Cell Validating(By Varing()) = False Then Message Box. Cancel = True End If End Select End Sub Which works just fine as far as finding an invalid date, gently alerting the user, and returning the focus to the offending cell.My question is, if the date field was initially NULL how do I set it back to Null ?? Value (If it's not Value, it's something similar, not at my dev machine at the moment, so pardon me - can't test :) ). You may be getting an error in your Cell Validating, you might want to check that the value Is Not DBNull. Jake OK, I got it working, but I'm not pleased with my solution.It seems that once the user enters any character, I'm sunk. Jake Scans your site and returns information about your SSL implementation and certificate. It seems that in the _Cell Validating event you must look at "e.

I've put in over 8 hours just trying to validate a stupid cell on a Data Grid View. :) :) Thanks for your help, I'll happily give you the points.Steve B2175, I'm sorry that you are having all this trouble.Incidently, thanks for the points, although I'd like to work with you a bit further on this...I'm thinking that rather than trying to do this via Current Cell, etc., we should try using the underlying datasource instead.I know how we can do this, however, I just need to know how you bind your datagrid... If you let me know, I'll write you a handler so that we can examine the value in your validating event, and if the date is not proper, we can examine the underlying datasource, and view the .Original Value of the column for the row, and if it was null, then we can simply reset that value to null.I'm just playing around right now with how best to do this...The only problem I am having at the moment is figuring out exactly which cell we're coming from (validating), since the currentcell is the cell we're moving to. Value End If This SHOULD work, but I'm so busy at the moment I haven't tested it... Value But returns an error stating that this is a read-only value.Let me know what you use for a datasource (If it is not Datarows; i.e., a dataset, datatable, dataview), then we may need to add some logic if your using objects. OK, what I have done is to create a dynamic SQL string and then bound it to the datagrid as shown below. Value Then ' The row was originally Null, so we can set the row to null. I know it works in VB 1.1, so it should work fine in 2.0, as I didn't get any "underlines" in my code :) Please let me know, Jake ource, dgv Top. Thanks again, I just can't believe that this is so very difficult, seems like it should be a common, simple situation that many developers run across.If we do it this way, you will be able to put your data back to the correct datatypes! I didn't include the SQL string because it is quite lengthy (joins 15 tables). Data Member), Currency Manager) But I'm still having the same problem that the only way I can find to look at the data in the current cell is to look at "e.formattedvalue". Jake, The answer to my question is quite simply "Ctrl 0" The root of the problem was if a user entered any data in a cell of the dgv that was not a date, there was no way to get off the cell.Also, I would be grateful to know the correct way to describe exactly what I've done (i.e. ) 'String is created, now load the Data Grid View Dim Tedia Connection String As String = "Data Source='" & Server & "'; Initial Catalog=FSDBB1; Integrated Security=False; User ID=xxxxxx; Pwd=xxxxxx" Dim Tedia Connection As New Sql Client. The data in the Data View does not reflect that is in the current cell, and I can't find a way to set the value of the current cell to DBNull. In my example, if the dgv returned a null in a date cell it would appear as a blank (and properly so).

106 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*