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

Calculating cell values based on cell changes in same row

Hi, I have Price, Qty and Amount columns in a databound grid with DataTable as data source. When ever price or Qty changes, I want to recalculate amount and show the new value in the same row. For this I have CellDeactivated event handler. Something is going wrong and it is adding blank rows all the time. 1. Please run the attached sample. 2. Click on Price cell and enter 55. Why am I getting an exception here? 3. Put in 5 for qty. It calculates amount properly. 4. Now click on the last row again. Enter 4 for Price and click on 5 first row, Qty column. Why is it adding a new row here? 5. When ever you click on LastRow-1 and click back on the row above it, it is adding a new row. I tried this on 1.6.06 and 1.5.2 as well. Samething happened. Can you look at the attached sample and let me know what is going on. thanks, - Reddy

1 Reply

AD Administrator Syncfusion Team June 18, 2003 03:29 PM UTC

CurrentCellDeactivated is not a good place to do this kind of thing. First it is called too often, and the is without an active cell at this point. The first exception, you are seeing is due to an initial DeactivateCell as the clicked cell is getting focus. After that, there are other exceptions being thrown by trying to parse as a double a value of DBNull. I think all these tings are contributing the weird new row behavior because the exceptions are being thrown as you click into a empty row. Anyway, I think you can tweak your code an dmove it to CurrentCellConfirmChanges, and get things to work without these problems. Here is the handler I tried.
private void gridDataBoundGrid1_CurrentCellAcceptedChanges(object sender, CancelEventArgs e)
{
	try
	{
		GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;
		int row = cc.RowIndex;
		int priceColIndex = gridDataBoundGrid1.NameToColIndex("Price");
		int qtyColIndex = gridDataBoundGrid1.NameToColIndex("Qty");
		int totalColIndex = gridDataBoundGrid1.NameToColIndex("Total");

		if(cc.ColIndex == priceColIndex || cc.ColIndex == qtyColIndex)
		{
			decimal price = ParseDecimal(gridDataBoundGrid1[row, priceColIndex].CellValue, 0.00M);
			decimal qty = ParseDecimal(gridDataBoundGrid1[row, qtyColIndex].CellValue, 0.00M);
			Console.WriteLine("Row="+row+", price="+price+", qty="+qty);
			Console.WriteLine("RowCount=" + gridDataBoundGrid1.Model.RowCount);
			gridDataBoundGrid1[row, totalColIndex].CellValue = price * qty;
			this.gridDataBoundGrid1.RefreshRange(GridRangeInfo.Cell(row, totalColIndex));
		}
	}
	catch(Exception ex)
	{
		Console.WriteLine("Error: " + ex);
	}
}
To handle the DBNull problem I added an etra condition to your parse code.
if(o != null && o != DBNull.Value)

Loader.
Live Chat Icon For mobile
Up arrow icon