Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > how to catch datagrid view error when entry is not a number
 

how to catch datagrid view error when entry is not a number

My application has a datagridview bound to a data table of numbers.  The data format of each column in the datagridview is N2, and the cells display a value correctly, (like 102.34).  I'm trying to capture an error where the user enters a string, or perhaps something like 32..43 instead of 32.43.  I'm trying to look at cell validating and inspect the cell data before letting it get updated.  I just want to send out some sort of text tool tip telling the user to enter a number, and not let him go on to the next cell.  Something like

Private

 

Sub FUELTABLEDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles FUELTABLEDataGridView.CellValidating

 

If e.FormattedValue ????
Thanks in advance

stewprince  Thursday, October 15, 2009 5:26 AM
Stew,

the datagridview has a dataerror event which you can handle this sort of thing.  i can't recall off hand the specifics.  in your validating sub you can use the tryparse method to check the value.

example
dim chknum as double
if double.tryparse(e.formattedvalue.tostring, chknum) then
'you have a correct number entry
else
'not correct
end if


for the error trapping i also have some projects on my website which can help you with this.  i can't recall if i have one that specifically deals with this situation but they do deal with null values and some other issues.  they can help you get some things figured out that you may need in the future.  i just have not looked at them in a while.  there is also another project in the database section which shows how to use a textbox as an editing control for a datagridview.  basically it overrides the default editing control and allows you to use your own textbox.  you can also modify it to use any other control you like.  can be very useful when you need to do complex data and error handling since it will allow you to take advantage of your controls built in events.  you can also handle the editing control showing event of the datagridview as well.  i have not played with it much so i can't tell you whether it is better to go one way or the other.
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
Jeff - www.SRSoft.us  Thursday, October 15, 2009 12:40 PM
Stew,

the datagridview has a dataerror event which you can handle this sort of thing.  i can't recall off hand the specifics.  in your validating sub you can use the tryparse method to check the value.

example
dim chknum as double
if double.tryparse(e.formattedvalue.tostring, chknum) then
'you have a correct number entry
else
'not correct
end if


for the error trapping i also have some projects on my website which can help you with this.  i can't recall if i have one that specifically deals with this situation but they do deal with null values and some other issues.  they can help you get some things figured out that you may need in the future.  i just have not looked at them in a while.  there is also another project in the database section which shows how to use a textbox as an editing control for a datagridview.  basically it overrides the default editing control and allows you to use your own textbox.  you can also modify it to use any other control you like.  can be very useful when you need to do complex data and error handling since it will allow you to take advantage of your controls built in events.  you can also handle the editing control showing event of the datagridview as well.  i have not played with it much so i can't tell you whether it is better to go one way or the other.
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial
Jeff - www.SRSoft.us  Thursday, October 15, 2009 12:40 PM
Thank you so much...it works great now.  I put it in the cell validating subroutine and it works perfectly.

Again, many thanks
Stew
stewprince  Friday, October 16, 2009 6:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Pushpin
• Vb.Net REGEX and HTML
• Searching example to link multiple categories to multiple topics in visual basic and sql compact
• How to use Modbus.dll from www.nmodbus.com
• Which Method Is Better?
• problem about login again after user click 'X' which on the top right of Window
• DataGridView "loses" formatting
• Font Problem
• Data type mismatch in criteria expression
• Help Text to ACCESS problem