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

Catching Exceptions before Cell value is saved

I'm using a GridDataBound grid binded to a collection. Once the cell value is saved in the grid (Syncfusion.Windows.Forms.Grid.GridModelDataBinder.SaveCellInfo(GridSaveCellInfoEventArgs e)) I have some rules in the domain model checking if the entered value is correct. If the cell value is wrong an exception is raised. I didn't find any place where I can catch those exceptions after/before the cell value is saved, so I can cancel any wrong input value. When I trace the execution process CurrentCellValidating and CurrentCellValidated are being thrown before the cell value is saved (Seem to me that I can not use them for I want). Also, Syncfusion.Windows.Forms.Grid.GridModelDataBinder.SaveCellInfo(GridSaveCellInfoEventArgs e) is protected so I'm not able to use that event. Any ideas how can I cancel a cell value before it is saved? Thanks

7 Replies

AD Administrator Syncfusion Team July 27, 2003 09:16 PM UTC

I don't know of an simple way to catch your exceptioo. But you can handle the grid.CurrentCellValidating event, test the value, and cancel it if necessary there.
//C#
private void gridControl1_CurrentCellValidating(object sender, CancelEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	string s = cc.Renderer.ControlText;
	GridStyleInfo style = this.gridControl1[cc.RowIndex, cc.ColIndex];
	if(s.Length > 0)
	{
		try
		{
			if(style.CellValueType == typeof(double))
			{
				double d = double.Parse(s);
			}
			//else check other types....
		}
		catch
		{
			e.Cancel = true;
			cc.ErrorMessage = "Your Error Message";
		}
	}
}


'VB.NET
Private Sub GridDataBoundGrid1_CurrentCellValidating(ByVal sender As Object, ByVal e As  CancelEventArgs) Handles GridDataBoundGrid1.CurrentCellValidating

	Dim cc As GridCurrentCell = Me.GridDataBoundGrid1.CurrentCell

       	Dim s As String = cc.Renderer.ControlText

	Try
		Dim d As Double = double.Parse(s)
	Catch
		'bad value
		e.Cancel = True
		cc.ErrorMessage = "MyMessage"
	EndTry
End Sub


AD Administrator Syncfusion Team July 28, 2003 11:06 AM UTC

> I don't know of an simple way to catch your exceptioo. But you can handle the grid.CurrentCellValidating event, test the value, and cancel it if necessary there. Here is the situation, I don't have any validation logic in the UI, everything resides in the domain model. Business Logic is very complex so I can't handle every single case in grid.CurrentCellValidating. So, let's supose I have a Customer collection binded to the grid, everytime the user change any property of the customer using the grid, the new value goes to the customer object and it's validated. If there is any problem an exception it's raised. As you can see there is no validation logic in the UI layer, what do you recommend in this case? Thanks


AD Administrator Syncfusion Team July 28, 2003 05:41 PM UTC

Jose, there is a DataProviderSaveCellInfo event raised just before SaveCellInfo is called. If you set e.Handled = true in the event handler for DataProviderSaveCellInfo you can implement your own logic to to store the cells value into your underlying list and validate it as needed. Will that work for you? Stefan


AD Administrator Syncfusion Team July 29, 2003 03:46 PM UTC

> Jose, > > there is a DataProviderSaveCellInfo event raised just before SaveCellInfo is called. If you set e.Handled = true in the event handler for DataProviderSaveCellInfo you can implement your own logic to to store the cells value into your underlying list and validate it as needed. > > Will that work for you? > > Stefan Hello Stefan, I have created a simple application where a GDBG is populated with a Customer Collection (CollectionBase, IBindingList, ITypedList). I also have a Customer and a CustomerPropertyDescriptor class. Try to change to D.O.B. of any of the Customers to 1/1/2004 in the grid, an exception is going to be thrown from the Customer object. Where should I catch the exception in order to cancel invalid cell values being validated in Customer objects? Thanks Jose


AD Administrator Syncfusion Team July 30, 2003 06:12 AM UTC

Jose, use the following event handlers: this.gridDataBoundGrid1.CurrentCellConfirmChangesFailed += new EventHandler(gridDataBoundGrid1_CurrentCellConfirmChangesFailed); this.gridDataBoundGrid1.ValidateFailed += new GridValidateFailedEventHandler(gridDataBoundGrid1_ValidateFailed); private void gridDataBoundGrid1_CurrentCellConfirmChangesFailed(object sender, EventArgs e) { if (gridDataBoundGrid1.CurrentCell.Exception != null) { MessageBox.Show("gridDataBoundGrid1_CurrentCellConfirmChangesFailed - Error occurred: " + this.gridDataBoundGrid1.CurrentCell.Exception.ToString()); gridDataBoundGrid1.CurrentCell.ResetError(); } } private void gridDataBoundGrid1_ValidateFailed(object sender, GridValidateFailedEventArgs e) { if (gridDataBoundGrid1.CurrentCell.Exception != null) { MessageBox.Show("Model_SaveCellInfo - Error occurred: " + this.gridDataBoundGrid1.CurrentCell.Exception.ToString()); gridDataBoundGrid1.CurrentCell.ResetError(); e.Handled = true; } } Also, change your customer DOB property setter to: public DateTime DOB { get {return dob;} set { if (value > DateTime.Now.Date) throw new Exception("Invalid D.O.B. " + value.ToString()); dob = value; } } (before you used dob to compare with DateTime.Now) If you want to delay validation of the customer record until the user actually leaves the current record, you should implement the IEditable interface for the Customer class. Stefan


AD Administrator Syncfusion Team July 30, 2003 06:17 AM UTC

Actually, it looks like handling only the ValidateFailed event is enough. You don't need to handle CurrentCellConfirmChangesFailed. Stefan


AD Administrator Syncfusion Team July 30, 2003 11:12 AM UTC

> Actually, it looks like handling only the ValidateFailed event is enough. > > You don't need to handle CurrentCellConfirmChangesFailed. > > Stefan Thanks... just what I needed....

Loader.
Live Chat Icon For mobile
Up arrow icon