Form Event AfterFieldChange Syntax

Wannabe-coder, lost in views and Scripting syntax, looking for help with correct code for AfterFieldChange

This customization adds a few columns from a BAQ to the Standard Data view for Sales Order > Build Order from history. I am now trying to add a new input field (NumPacks) such that they can change that value, and it will do the math and fill in the standard Epicor Value (NewQty). I started by adding a field to my BAQ that returns a Zero, and making it ReadOnly False, seems fine.

My goal, is that IF someone changes that field, it will do the math multiplying NumPacks * Part_Number05 and replace the proposed value for NewQty.

At the very bottom of the script below you will see my ham-handed attempt to stuff an integer into NewQty, so I’m clearly out of my depth. Can anyone provide the code to replace that line commented with ‘I WANT TO SET…’

// **************************************************
// Custom code for OrderHistoryForm
// Created: 11/19/2019 2:00:26 PM
// **************************************************
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using System.Linq;
public class Script
{
// ** Wizard Insert Location - Do Not Remove ‘Begin/End Wizard Added Module Level Variables’ Comments! **
// Begin Wizard Added Module Level Variables **

// End Wizard Added Module Level Variables **

// Add Custom Module Level Variables Here **
DataTable searchDt; //Holds my search paramters (order numb, customer num)
EpiDataView edvSearch; //EpiDataView for the above table.
BAQDataView bdvSalesHistory; //BAQDAtaView for pulling in the history records

private OrderHistAdapter oTrans_adapter;
private EpiUltraGrid orderHistGrid;
public void InitializeCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
	// Begin Wizard Added Variable Initialization
	
	this.OrderHist_Column.ColumnChanged += new DataColumnChangeEventHandler(this.OrderHist_AfterFieldChange);
	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls

	
	// End Wizard Added Custom Method Calls
	this.oTrans_adapter = ((OrderHistAdapter)(this.csm.TransAdaptersHT["oTrans_adapter"]));
	CreateSearchDataView();
	orderHistGrid = csm.GetNativeControlReference("7908565c-cc3c-43d1-b85b-9921ad88fb89") as EpiUltraGrid;
	orderHistGrid.InitializeRow += new Infragistics.Win.UltraWinGrid.InitializeRowEventHandler(this.orderHist_InitializedRow);
	SetExtendedProperties();
}

public void DestroyCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
	// Begin Wizard Added Object Disposal

	this.OrderHist_Column.ColumnChanged -= new DataColumnChangeEventHandler(this.OrderHist_AfterFieldChange);
	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	// End Custom Code Disposal
	orderHistGrid.InitializeRow -= new Infragistics.Win.UltraWinGrid.InitializeRowEventHandler(this.orderHist_InitializedRow);
}

private void CreateSearchDataView()
{
	searchDt = new DataTable();
	searchDt.Columns.Add(new DataColumn("OrderNum", typeof(int)));
	searchDt.Columns.Add(new DataColumn("CustNum", typeof(int)));
	searchDt.Columns.Add(new DataColumn("SysRowID", typeof(Guid)){DefaultValue = Guid.NewGuid()});
	searchDt.TableName="SearchDv";

	//Add any new columns in the BAQ here
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("OrderDtl_XPartNum",typeof(String)));
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_Number05",typeof(decimal)));
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_ShortChar01",typeof(String)));
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_Number03",typeof(decimal)));
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_PartOnHold",typeof(bool)));
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_ShortChar03",typeof(String))); //Rick add 25-Nov-2019
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_Number01",typeof(decimal))); //Rick add 25-Nov-2019
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("Part_Number02",typeof(decimal))); //Rick add 25-Nov-2019
	oTrans_adapter.OrderHistData.OrderHist.Columns.Add(new DataColumn("NumPacks",typeof(int))); //Rick add 29-Sept-2020



	//Getting a hold of launch form options to extract customer number and order number
	var lfo = OrderHistoryForm.LaunchFormOptions;
	CompoundKeyBinding ckb = lfo.ValueIn as CompoundKeyBinding;
	int orderNum=0;
	int custNum =0;
	if (ckb != null)
    {
		var iEnum = ckb.CompoundKeys.GetEnumerator();
        custNum = int.Parse(ckb.CompoundKeys[0]);
		orderNum = int.Parse(ckb.CompoundKeys[1]);
		var row = searchDt.NewRow();
		row["OrderNum"]=orderNum;
		row["CustNum"]=custNum;
		searchDt.Rows.Add(row);
	}

	edvSearch = new EpiDataView();
	edvSearch.dataView = searchDt.DefaultView;
	oTrans.Add("SearchDv",edvSearch);


	//BAQ DataView being added to the current form for BAQID: BuildOrderFromHistory
	bdvSalesHistory = new BAQDataView("BuildOrderFromHistory");
	oTrans.Add("CustomerHistory", bdvSalesHistory);

	
	var customerNumBinding = "SearchDv.CustNum";
    

    
    oTrans.PublishColumnChange(customerNumBinding, Guid.NewGuid().ToString());
	
	var customerNumPub =  oTrans.GetPublisher(customerNumBinding);

	
	bdvSalesHistory.SubscribeToPublisher(customerNumPub.PublishName, "OrderDtl_CustNum");

	
	customerNumPub.PublishInitialValue(custNum.ToString(),true);
	
}

private void orderHist_InitializedRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs args)
{
	var row = bdvSalesHistory.dataView.Table.Select(string.Format("OrderDtl_OrderNum = {0} and OrderDtl_OrderLine = {1}",args.Row.Cells["OrderNum"].Value,args.Row.Cells["OrderLine"].Value)).FirstOrDefault();
	if(row!=null)
	{
		//Add any new columns in the BAQ here
		//Left Side is my custom column = Right Side is BAQ Column Name
		
		args.Row.Cells["OrderDtl_XPartNum"].Value = row["OrderDtl_XPartNum"];
		args.Row.Cells["Part_Number05"].Value = row["Part_Number05"];
		args.Row.Cells["Part_ShortChar01"].Value = row["Part_ShortChar01"];
		args.Row.Cells["Part_Number03"].Value = row["Part_Number03"];
		args.Row.Cells["Part_PartOnHold"].Value = row["Part_OnHold"];
		args.Row.Cells["Part_ShortChar03"].Value = row["Part_ShortChar03"]; //Rick add 25-Nov-2019
		args.Row.Cells["Part_Number01"].Value = row["Part_Number01"]; //Rick add 25-Nov-2019
		args.Row.Cells["Part_Number02"].Value = row["Part_Number02"]; //Rick add 25-Nov-2019
		args.Row.Cells["NumPacks"].Value = row["# of Packs"]; //Rick add 29-Sept-2020

	}
}

private void SetExtendedProperties()
{
	// Begin Wizard Added EpiDataView Initialization
	EpiDataView edvorderHist = ((EpiDataView)(this.oTrans.EpiDataViews["orderHist"]));
	// End Wizard Added EpiDataView Initialization

	// Begin Wizard Added Conditional Block
	if (edvorderHist.dataView.Table.Columns.Contains("Part_Number03"))
	{
		// Begin Wizard Added ExtendedProperty Settings: edvorderHist-Part_Number03
		//Add any new columns in the BAQ here
		edvorderHist.dataView.Table.Columns["Part_Number03"].ExtendedProperties["ReadOnly"] = true;
		edvorderHist.dataView.Table.Columns["Part_Number05"].ExtendedProperties["ReadOnly"] = true;
		edvorderHist.dataView.Table.Columns["Part_ShortChar01"].ExtendedProperties["ReadOnly"] = true;
		edvorderHist.dataView.Table.Columns["Part_PartOnHold"].ExtendedProperties["ReadOnly"] = true;
		edvorderHist.dataView.Table.Columns["OrderDtl_XPartNum"].ExtendedProperties["ReadOnly"] = true;
		edvorderHist.dataView.Table.Columns["Part_ShortChar03"].ExtendedProperties["ReadOnly"] = true; //Rick add 25-Nov-2019
		edvorderHist.dataView.Table.Columns["Part_Number01"].ExtendedProperties["ReadOnly"] = true; //Rick add 25-Nov-2019
		edvorderHist.dataView.Table.Columns["Part_Number02"].ExtendedProperties["ReadOnly"] = true; //Rick add 25-Nov-2019
		edvorderHist.dataView.Table.Columns["NumPacks"].ExtendedProperties["ReadOnly"] = false; //Rick add 29-Sept-2020
		// End Wizard Added ExtendedProperty Settings: edvorderHist-Part_Number03
	}
	// End Wizard Added Conditional Block
}

private void OrderHist_AfterFieldChange(object sender, DataColumnChangeEventArgs args)//Rick add 29-Sept-2020
{
	// ** Argument Properties and Uses **
	// args.Row["FieldName"]
	// args.Column, args.ProposedValue, args.Row
	// Add Event Handler Code
	switch (args.Column.ColumnName)
	{
		case "NumPacks":
		// args.ProposedValue.NewQty = 789; - //I WANT TO SET  'NewQty' which is a native field in Epicor, to 'NumPacks * Part_Number05'
			break;
	}
}

}

1 Like

I didn’t go through all the code to se exactly what you’re doing. BUt from your description, it sounds like a UOM conversion would work - and be far more native to E10.

Also, please put three ticks on a line by themselves, before and after the code in your post

That describes me as well. I just happened to have a knack for making reasonable guesses. :wink:

My guess here would be that you need to create a data view in the .._AfterFieldChange() event handler. Then use values in the args parameter to set which row of the data set you’re updating.

Thanks Calvin, I appreciate the input. Unfortunately I only 75% understand what you are saying in conversation, and .0005% understand how to write the code. We may have to hire this one out. Oh wait, I see you sent an example, thanks! I’ll look at that!

I think its even easier than I tried to describe.

Try this:

private void OrderHist_AfterFieldChange(object sender, DataColumnChangeEventArgs args)//Rick add 29-Sept-2020
	{
		// ** Argument Properties and Uses **
		// args.Row["FieldName"]
		// args.Column, args.ProposedValue, args.Row
		// Add Event Handler Code
		switch (args.Column.ColumnName)
		{
			case "NumPacks":
			EpiDataView edvOHist = ((EpiDataView)(this.oTrans.EpiDataViews["OrderHist"]));
			edvOHist.dataView[edvOHist.Row]["NewQty"] = Convert.ToDecimal(args.ProposedValue) * (decimal)edvOHist.dataView[edvOHist.Row]["Part_Number05"];
			break;
		}
	}
Picture of code (no word wrapping)

I made some assumptions on the datatypes for NewQty, and Part_Number05. You’ll probably have to play with the castings (the (Int32) before a variable) and/or conversions (the Convert.TiInt32() used to get a value from args.ProposedValue

EDIT

I updated the code above (both the text and picture) for the proper datatypes. I’m assuming yourPart_Number05 is a decimal.

Thank you for hanging with me….

Good news is that your code compiles fine, but it wasn’t working. I tried to dumb it down before building back up to formulas. Below my testing goal was, If you change NumPacks, then stuff 789 into NewQty. Again it complies, but doesn’t work. Or at least doesn’t refresh.

I also tried

edvOHist.dataView[edvOHist.Row][“NewQty”] = Convert.ToDecimal(789);

Since the “NumPacks” column is allinged to the left, I’m guessing it’s not a number (int’s and decimals normally align to the right of a cell). Same with “PackQty”.

Maybe try using

Convert.ToDecimal(edvOHist.dataView[edvOHist.Row]["Part_Number05"])

instead of

(decimal)edvOHist.dataView[edvOHist.Row]["Part_Number05"]

Firstly, I thought I took Number of packs out of the equation in my simplified goal of stuffing a set number in.

Secondly Up above in the code, NumPacks would seem to be an integer. My experience with alignment of numbers has been mixed. I agree that they should align right

While NumPacks my be an integer, you’ll want to convert it to decimal before multiplying it with edvOHists...["PartNumber05"]

As for your debugging, try
edvOHist.dataView[edvOHist.Row]["NewQty"] = 123.45;

Just to see if it really needs a decimal.

FWIW - my variation of the code works (my difference being I don’t have Part_Number05 or PackQty)

In the following I use LineNum as the input and update NewQty as LineNum * OrderQty. Sometimes you have to tell the program what type it is, and others what type you want to treat it as. In this case, it’s the latter.

edv

My event code is:

1 Like

Thank you again….

Your Gif proves that you know what I’m trying to do, thank you!

I can’t get the result shown in your Gif, nor can I get the simple 123.45 debug to work.

I’m going to put this one down for tonight. Thank you again for your kind efforts.

Regards,

Rick

FWIW - my example uses the forms original dataset, without the added columns like you did.

To make sure that your event is firing, add the inside the case "NumPacks":

MessageBox.Show("args.PropVal: " + Convert.ToDecimal(args.ProposedValue));

When you tab out of that field, the message box should show. If it doesn’t, put the following before the switch() statement:

MessageBox.Show("args.Col.ColName: " + args.Column.ColumnName);

If that message doesn’t show, then the event isn’t firing at all. If it does show but with something other than “NumPacks”, then that’s what the case value should use.