Programmer Item Maintenance: Item Batch Table OnColumnChange event v2

From AutoCount Resource Center
Jump to navigation Jump to search

Version Info

  • AutoCount Accounting 2.0
  • Tested version: 2.0.1.100

Introduction

In Item Maintenance, when changes of data is made to the form, two events below are triggered

public void OnItemColumnChanged(AutoCount.Stock.Item.StockItemColumnChangedEventArgs e)
public void OnItemUomColumnChanged(AutoCount.Stock.Item.StockItemUomColumnChangedEventArgs e)

However, these two events that are provided for the value changed of the column is on "Master" and "Detail" tables.
Item Batch data is not held in neither of these two tables.
Therefore, to capture the data changes of Item Batch table, programmer can add the OnColumnChanged event to the Item Batch table.

Sample code to add events to Item Batch table

This sample code illustrates how programmer can locate the DataTable of Item Batch.
And create OnColumnChanged event when it successfully finding the Item Batch Table.

Add the event OnFormInitialize at Item Maintenance

public void OnFormInitialize(object sender, AutoCount.Stock.Item.FormStockItem.LayoutFormInitializeEventArgs e)
{
    CreateItemBatchTableColumnChangedEvent(e);
}

Create the event OnColumnChanged for Item Batch

private void TblItemBatch_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
    if (e.Column.ColumnName == "BatchNo")
    {
        if (e.ProposedValue.Equals("") || e.ProposedValue.Equals(DBNull.Value))
        {
            //Do something when BatchNo is empty
            return;
        }
        else
        {
            //Do something when BatchNo is not empty
            AutoCount.AppMessage.ShowMessage(
                string.Format("BatchNo is changed to {0}.\nDescription:{1}",
                e.ProposedValue, e.Row["Description"]));
        }
    }
}

//Method Entry Point
private void CreateItemBatchTableColumnChangedEvent(AutoCount.Stock.Item.FormStockItem.LayoutFormInitializeEventArgs e)
{
    //Get the GridControl that holds the DataTable of Item Batch
    DevExpress.XtraGrid.GridControl gcItemBatch = GetItemBatchGridControl(e);

    if (gcItemBatch != null && gcItemBatch.DataSource != null && gcItemBatch.DataSource is DataTable)
    {
        DataTable tblItemBatch = gcItemBatch.DataSource as DataTable;

        //Create Item Batch Table event
        tblItemBatch.ColumnChanged += TblItemBatch_ColumnChanged;
    }
    else
    {
        AutoCount.AppMessage.ShowMessage("Fail to add Column Change to ItemBatch");
    }
}

Find the GridControl in the TabPage of e.TabControlMaster

private DevExpress.XtraGrid.GridControl GetItemBatchGridControl(AutoCount.Stock.Item.FormStockItem.LayoutFormInitializeEventArgs e)
{
    DevExpress.XtraLayout.LayoutGroup grpTabItemBatch = null;
    DevExpress.XtraLayout.LayoutControlItem layoutItemBatch = null;
    string findTabPageName = "lygItemBatch";

    //Find the TabPage of "Item Batch"
    for (int i = 1; i < e.TabControlMaster.TabPages.Count; i++)
    {
        if (e.TabControlMaster.TabPages[i].Name == findTabPageName)
        {
            grpTabItemBatch = e.TabControlMaster.TabPages[i];
        }
    }

    if (grpTabItemBatch == null)
    {
        AutoCount.AppMessage.ShowMessage("Item Batch Tab Page not found.");
        return null;
    }
    else
    {
        //If grpTabItemBatch.Items[0] is not LayoutControlItem, the object will be null
        if (grpTabItemBatch.Items.Count > 0)
        {
            layoutItemBatch = grpTabItemBatch.Items[0] as DevExpress.XtraLayout.LayoutControlItem;
        }

        //If layoutItemBatch.Control.Controls[0] is not GridControl, the object will be null
        if (layoutItemBatch != null && layoutItemBatch.Control.Controls.Count > 0)
        {
            return layoutItemBatch.Control.Controls[0] as DevExpress.XtraGrid.GridControl;
        }
        else
        {
            AutoCount.AppMessage.ShowMessage("Item Batch Grid Control not found.");
            return null;
        }
    }
}

Go to menu

IconAC81.png Go to top
ProgrammerGo.jpg Resources For AutoCount Software Developers