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.
Unfortunately, activation email could not send to your email. Please try again.

Calculating cell values based on cell changes in same row

Thread ID:

Created:

Updated:

Platform:

Replies:

5084 Jun 18,2003 10:10 AM Jun 18,2003 11:29 AM Windows Forms 1
loading
Tags: GridControl
Administrator [Syncfusion]
Asked On June 18, 2003 10:10 AM

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

Administrator [Syncfusion]
Replied On June 18, 2003 11:29 AM

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)

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;