We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exception for wrong data type

Hi together, I have a gridgrouping control which is bound to a dataset (which contains multiple tables). There are columns of type "double". Now when I enter a string (e.g. "test") instead of a number, I get an exception like this: catched at Syncfusion.Windows.Forms.Grid.GridCellRendererBase.OnValidate() in :line 0 How can I prevent such an exception in a safe manner. Cheers, Markus

33 Replies

AD Administrator Syncfusion Team September 6, 2005 03:03 PM UTC

Hi Markus You can make use of the TableControlCurrentCellMoveFailed event and TableControlCurrentCellValidating events to prevent the eceptionin the safe manner. Here is the code snippet. private void gridGroupingControl1_TableControlCurrentCellValidating(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCancelEventArgs e) { if(e.TableControl.CurrentCell.ColIndex == 2) { e.Inner.Cancel = true; e.TableControl.CurrentCell.ErrorMessage = "Invalid Datatype"; } } private void gridGroupingControl1_TableControlCurrentCellMoveFailed(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlCurrentCellMoveFailedEventArgs e) { if(e.TableControl.CurrentCell.Renderer.ColIndex == 2) { e.TableControl.CurrentCell.Lock(); MessageBox.Show("Invalid Datatype,Cannot Move"); e.TableControl.CurrentCell.Unlock(); e.TableControl.CurrentCell.ErrorMessage = ""; } }


AD Administrator Syncfusion Team September 6, 2005 03:04 PM UTC

>Hi together, > >I have a gridgrouping control which is bound to a dataset (which contains multiple tables). >There are columns of type "double". Now when I enter a string (e.g. "test") instead of a number, I get an exception like this: > >catched at Syncfusion.Windows.Forms.Grid.GridCellRendererBase.OnValidate() in :line 0 > >How can I prevent such an exception in a safe manner. > >Cheers, >Markus


AD Administrator Syncfusion Team September 6, 2005 03:08 PM UTC

That is a first chance exception that is normally handled by the grid and an error message is displayed. If you want to catch things before the first chance exception is thrown, then you can handle the TableControlCurrentCellValidating event. There check CurrentCell.Renderer.ControlText to see if it holds a valid value for the currentcell being worked with. If you want to just catch the error window, you can handle TabeControlCurrentCellMovedFailed. There you can set CurrentCell.ErrorMessage to display some other message (or set it to an empty string to display no message).


AD Administrator Syncfusion Team September 7, 2005 08:03 AM UTC

Ok thanks, this seems to work - but how can I get to the corresponding column of my DataTable object (to get the type of the column). It seems that e.TableControl.CurrentCell.ColIndex is not the corresponding column number of my real DataTable (so if e.TableControl.CurrentCell.ColIndex gives a "2" the real column index is "1"). Is it because the rowheader column?? Cheers, Markus >That is a first chance exception that is normally handled by the grid and an error message is displayed. > >If you want to catch things before the first chance exception is thrown, then you can handle the TableControlCurrentCellValidating event. There check CurrentCell.Renderer.ControlText to see if it holds a valid value for the currentcell being worked with. > >If you want to just catch the error window, you can handle TabeControlCurrentCellMovedFailed. There you can set CurrentCell.ErrorMessage to display some other message (or set it to an empty string to display no message).


AD Administrator Syncfusion Team September 7, 2005 08:09 AM UTC

You can get it through the GridTableCellStyleInfo object for the current cell. GridCurrentCell cc = e.TableControl.CurrentCell; GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(cc.RowIndex, cc.ColIndex); GridColumnDescriptor cd = style.TableCellIdentity.Column; string name = cd.Name;


AD Administrator Syncfusion Team September 7, 2005 08:33 AM UTC

Ok that works - but why is the event TableControlCurrentCellValidating fired twice? Cheers, Markus


AD Administrator Syncfusion Team September 7, 2005 09:06 AM UTC

It should only be raised once as you leave an edited cell as in your 4tables sample. If the validation fails, you will continually hit (until the validation does not fail) the event as you try to leave the cell. http://www.syncfusion.com/Support/user/uploads/GGC_4tables_6946dee6.zip


AD Administrator Syncfusion Team September 7, 2005 09:23 AM UTC

Hmm, here is my example where you can see that the event is fired twice. Perhaps it has something to do with a workaround from you?? GridException_witValidation_6057.zip Any ideas? Cheers, Markus


AD Administrator Syncfusion Team September 7, 2005 09:25 AM UTC

forgot to say that you must modify column "A" in the first table to see the event fired twice (just enter a string - not a number).


AD Administrator Syncfusion Team September 7, 2005 01:54 PM UTC

Using 3.3, I type abc in the very first cell and try to move to the second cell in the same row, I only see one message at that point. Is this where you see 2 messages? What version of our libraries are you using?


AD Administrator Syncfusion Team September 7, 2005 02:04 PM UTC

Ok you have to collapse table "Test 1" and then enter in the "A" column e.g. "abc". Now press KEY DOWN and you will see the messagebox twice. Cheers, Markus


AD Administrator Syncfusion Team September 7, 2005 06:45 PM UTC

I think I get this problem because of the workaround, which you told me to use in this thread: http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=34007 Do you think it''s possible to avoid the event fired twice? Any help would be nice, because I can''t validate my cell''s in a safe manner. Cheers, Markus


AD Administrator Syncfusion Team September 7, 2005 07:22 PM UTC

The cc.ConfirmChanges call in your TableControlCurrentCellKeyDown is raising the CurrentCellValidating event. This is by design. You can avoid the double call by setting flag in CellKeyDown event and then test this flag in the CurrentCellValidating event. private bool validationAlreadyCalled = false; void gridGroupingControl1_TableControlCurrentCellKeyDown(object sender, GridTableControlKeyEventArgs e) { validationAlreadyCalled = false; if (e.Inner.KeyCode == Keys.Up || e.Inner.KeyCode == Keys.Down) { GridCurrentCell cc = e.TableControl.CurrentCell; GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(cc.RowIndex, cc.ColIndex); Element rec = style.TableCellIdentity.DisplayElement; if (rec != null) { DataRowView drv = rec.GetData() as DataRowView; validationAlreadyCalled = drv != null; string field = ""; if (drv != null && cc.ConfirmChanges() && !this.ValidateDataRow(drv.Row, out field)) { e.Inner.Handled = true; if (rec is AddNewRecord) { ((AddNewRecord)rec).SetCurrent(field); } else if (rec is GridRecord) { ((GridRecord)rec).SetCurrent(field); } } } } } void gridGroupingControl1_TableControlCurrentCellValidating(object sender, GridTableControlCancelEventArgs e) { if (!validationAlreadyCalled) { GridCurrentCell cc = e.TableControl.CurrentCell; GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(cc.RowIndex, cc.ColIndex); GridColumnDescriptor cd = style.TableCellIdentity.Column; string ll = e.TableControl.CurrentCell.Renderer.ControlText; string tableName = e.TableControl.TableDescriptor.Name; DataTable actTable = (gridGroupingControl1.DataSource as DataSet).Tables[tableName]; Type actType = actTable.Columns[cd.Name].DataType; try { Convert.ChangeType(ll, actType); } catch (Exception err) { MessageBox.Show("Wrong value for that type"); e.Inner.Cancel = true; } } validationAlreadyCalled = false; }


AD Administrator Syncfusion Team September 8, 2005 06:21 AM UTC

Ok Clay - thank''s for that, it''s hard when I don''t have the source to find the call which is responsible for the event fired twice - But now the event is only called one time. But there are still exception which can''t be handled in a safe manner. When I run the example (in debug mode) and change the column "A" of the first table to a literal value I get the following exception in the Quick Console windows of the visual studio: catched at Syncfusion.Windows.Forms.Grid.GridCellRendererBase.OnValidate() in :line 0 What does that mean - I think I handle the validation now by my own, so this exception shouldn''t occur or?? I have attached the project. Cheers, Markus GridValidation_5394.zip


AD Administrator Syncfusion Team September 8, 2005 07:06 AM UTC

In your TableControlCurrentCellKeyDown event, inside the IF statement that catches the arrow keys, set e.Inner.handled = true. The code is trying to still process the arrow key after the event handler returns, and that is why you are still seeing OnValidate called again.
if (e.Inner.KeyCode == Keys.Up || e.Inner.KeyCode == Keys.Down)
{
	e.Inner.Handled = true;


AD Administrator Syncfusion Team September 8, 2005 08:46 AM UTC

So this prevents the exception but I am not able to add a new row with the KEY DOWN or KEY UP keys to the grid. Only when I click with the mouse to a different cell then the new row is added. So what can be done to add the new row to the grid with KEY UP or KEY DOWN? Cheers, Markus


AD Administrator Syncfusion Team September 8, 2005 09:02 AM UTC

Addition to my last posting above - when the first column gets an error while validating, and then the user enters a correct value (with addition press the RETURN key) the next hightlighted column is not "B" instead it jumps directly to "C". This is also strange??? Cheers, Markus


AD Administrator Syncfusion Team September 8, 2005 04:14 PM UTC

Hmm just want recall the thread - because I can''t really validate the cell content in a safe manner. Cheers, Markus


AD Administrator Syncfusion Team September 8, 2005 04:19 PM UTC

You have not been ignored. Don''t really have a suggestion for this yet. The problem has to do with trying to handle sibling tables. We will have to make changes in our library to fully support this. But as time permits, I will continue trying to tweak the code to see if there is some way of miminally handling this problem without library changes.


AD Administrator Syncfusion Team September 8, 2005 05:16 PM UTC

Hi Clay - thanks for your help!!! Hope you find a workaround.


AD Administrator Syncfusion Team September 9, 2005 07:36 AM UTC

Here is your project back. http://www.syncfusion.com/Support/user/uploads/GridException_7fd90d43.zip We made some changes in our libraries to handle some problems with multiple sibling tables. With the tweaked library code, the above project was tweaked to handle the problems you mentioned above. To get a private build of our library with these changes in it, you can submit a Direct Trac support incident


AD Administrator Syncfusion Team September 9, 2005 08:51 AM UTC

Hi Clay, thanks for your great support!!! Cheers, Markus


AD Administrator Syncfusion Team September 12, 2005 06:03 AM UTC

Hi Clay, I have made a DirectTrac request (No. 20869) - but I didn''t get any download link yet? Cheers, Markus


AD Administrator Syncfusion Team September 12, 2005 02:38 PM UTC

Hi Markus, We have updated your Incident with the latest Assemblies. Best regards, Jay N


AD Administrator Syncfusion Team September 13, 2005 06:39 AM UTC

Hi, thanks for the package - but there is the Syncfusion.Core assembly missing. I have updated the direct-trac request. Cheers, Markus


AD Administrator Syncfusion Team September 13, 2005 10:16 AM UTC

Hi Markus, I have updated your incident with the details. Best regards, Jay N


AD Administrator Syncfusion Team September 13, 2005 12:06 PM UTC

Hmm, ok I have tried your bugfix with the project mentioned by Clay: http://www.syncfusion.com/Support/user/uploads/GridException_7fd90d43.zip Now when I try to append a new row with cursor key down - I get an exception in the ValidateCurrentCell function. Besides when i enter a "wrong" value (literal) in column "A" of the first table I get a message ("wrong type") - this is correct. But when I then enter a correct value (number) the highlighted rectangle jumps directly to column "C" instead of column "B" - so it overjumps column "B". Any ideas? Cheers, Markus


AD Administrator Syncfusion Team September 13, 2005 11:46 PM UTC

With the new dll''s (dated 9/08), I do not see the problem you mentioned. I type abc in the first cell, press downarrow, type 123 to correct it an dpress enter, and the value moves to the next cell (not skipping a cell).


AD Administrator Syncfusion Team September 14, 2005 06:14 AM UTC

Ok Clay, I try to install the 3.3.0.0 and than your bugfix - to see if it works. I actually work with 3.2.1.0 and the bugfix, perhaps this is the problem.


AD Administrator Syncfusion Team September 14, 2005 02:31 PM UTC

That is the problem. We modified our library to make this work. And the modification was done after the 3.3 release. So, you will have to use the private build that we furnished to get this to work. I do not know of any way to get this working without the change in our library.


AD Administrator Syncfusion Team September 15, 2005 06:59 AM UTC

Hi Clay, I still get the same error - I think I have got the wrong assemblies via direct-trac. My assembly version of the grid components is 3.302.0.0 and this is just the same version number as it is in the main 3.3 release. So I think I didn''t get the modified assemblies - What do you think? Is it possible to get the modified assemblies? The direct-trac number is 20869. Thank you in advance, Markus


AD Administrator Syncfusion Team September 15, 2005 07:15 AM UTC

Ok as it seems the cell overjump problem is solved even in the general 3.3.0.0 release. But in the example above, when you fill all the columns in table "Test 1" with correct values, and then press the downarrow key you will get an exception. I think it has something to do with that the cell validation function is called even on the group caption cell? Cheers, Markus


AD Administrator Syncfusion Team September 16, 2005 05:55 AM UTC

Hmm, just want to bring this tread in mind again - I am still suffering in validating the cell in my nested table solution. The exception I mentioned above is just new since the bugfix. Any ideas?? Cheers, Markus

Loader.
Live Chat Icon For mobile
Up arrow icon