Page 1 of 2

Lock a sales order line

PostPosted: Sat Aug 14, 2021 6:00 pm
by DannyC
I want to lock a sales order line from any editing.

I've tried the following but it doesn't work. Obviously removing the comments ;)
Code: Select all
foreach (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderLine line in salesOrderForm.SalesOrder.SalesOrderLines)
{
   if (!line.CommentLine && line.QuantityDemand > 0)
   {
      //salesOrderForm.grdLines.LockRow(line.ItemNo, true);               doesn't work
      //salesOrderForm.grdLines.ActiveSheet.ActiveRow.Locked = true;      doesn't work either      
   }
}


Any other tip?

Re: Lock a sales order line

PostPosted: Sun Aug 15, 2021 11:07 am
by SBarnes
That is because LockRow takes a first parameter of the row in the grid not the line item number, if you loop over the actual grid and then get the line by record id and get the value from the Key column and then check your conditions it will work below is how to get the key value.

What I am saying is the link between the lines and the grid is line's ID in the collection and the first hidden column in the grid where Jiwa puts the ID when it displays the lines. Jiwa does this for nearly all grids, you just have to work out what that first column is which you can usually find by looking at the setup function in the form.

Code: Select all
string LineID = salesOrderForm.grdLines.get_GridText("Key",row).ToString();


to get the line use


Code: Select all
SalesOrderLine line = salesOrderForm.SalesOrder.SalesOrderLines[LineID];

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 9:58 am
by DannyC
So what's the difference between your line
Code: Select all
SalesOrderLine line = salesOrderForm.SalesOrder.SalesOrderLines[LineID];


and my line?
Code: Select all
foreach (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderLine line in salesOrderForm.SalesOrder.SalesOrderLines)


It's the same line isn't it?
The first parameter of salesOrderForm.grdLines.LockRow takes an integer.

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 10:21 am
by SBarnes
There is no difference but if you use your for each loop you are then going to need a loop to roll over the grid inside it, basically you will end up with one loop if you look up the id of the line from the key column, below is the code for LockRow, the first value must be valid as the row you want to lock all it then does is go over the columns and locks the cells. The sales order line number also has nothing to do with the row number in the grid.


Code: Select all
        public void LockRow(int Row, bool LockValue)
        {
            IEnumerator enumerator = null;
            try
            {
                enumerator = ((IEnumerable)base.ActiveSheet.Columns).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Column current = (Column)enumerator.Current;
                    base.ActiveSheet.Cells[Row, current.Index].Locked = LockValue;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 11:50 am
by DannyC
The sales order line number also has nothing to do with the row number in the grid.

OK. But then by that reasoning this should work. It doesn't.
Code: Select all
for(int row = 0; row <= salesOrderForm.grdLines.ActiveSheet.RowCount - 1; row++)
{
   salesOrderForm.grdLines.LockRow(row, true); 
}

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 11:50 am
by Mike.Sheen
Another important aspect is to where you are performing this locking of cells / rows.

We performing locking / unlocking of cells as part of the DisplayLine in the form - which is called in reaction to all sorts of events... and then permissions set might also lock grid columns.

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 12:04 pm
by DannyC
We performing locking / unlocking of cells as part of the DisplayLine in the form

Ahhh! That might be the hint I'm looking for! Cheers Mike.

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 12:08 pm
by SBarnes
Of course the other way of doing things would be to not lock the line but add a change event handler for the grid in the set up before handlers and simply tell the user they can't change the line and then simply client cancel exception and then Jiwa will never know the user did anything.

Not quite as elegant as locking the row but it would achieve what you need fairly simply.

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 12:20 pm
by SBarnes
I just remembered I had to do a slight of hand to let certain columns be editable on a snapshot greater than 1 adding a change event to grid behind Jiwa worked.

I can't see why doing the same to lock columns wouldn't work as easily as unlocking them

Re: Lock a sales order line

PostPosted: Mon Aug 16, 2021 1:05 pm
by DannyC
I think I have it sussed.....basically I reckon I've possibly determined that LockRow doesn't work. Might need more testing to check.

I need to use LockColumn and just specify each column, kinda like the plugin Sales Orders - Round Cash Sales to Nearest 5c.
In that plugin is
Code: Select all
   public void SalesOrderForm_LineDisplayed(object sender, System.EventArgs e, JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderLine SalesOrderLine, int Row)
   {
      JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.BaseSalesOrderEntryForm salesOrderForm = (JiwaFinancials.Jiwa.JiwaSalesUI.SalesOrder.BaseSalesOrderEntryForm)sender;
      if (SalesOrderLine.PartNo == "ZRounding" && SalesOrderLine.NonStock)
      {
         for(int row = 0; row <= salesOrderForm.grdLines.ActiveSheet.RowCount - 1; row++)
         {
            if (salesOrderForm.grdLines.get_GridText("Key", row).ToString() == SalesOrderLine.RecID)
            {
               salesOrderForm.grdLines.LockColumn(true, "UnitOfMeasure", row);
               salesOrderForm.grdLines.LockColumn(true, "QuantityUnitOfMeasure", row);
               salesOrderForm.grdLines.LockColumn(true, "QuantityOrdered", row);
               salesOrderForm.grdLines.LockColumn(true, "QuantityBackOrdered", row);
               salesOrderForm.grdLines.LockColumn(true, "QuantityDemand", row);
               salesOrderForm.grdLines.LockColumn(true, "QuantityThisDelivery", row);
               salesOrderForm.grdLines.LockColumn(true, "SellPrice", row);
               salesOrderForm.grdLines.LockColumn(true, "PriceIncGST", row);
               salesOrderForm.grdLines.LockColumn(true, "ExtPrice", row);
               break;
            }
         }
      }
   }


so I just used that same concept & it seems to be doing what I was hoping.

EDIT: So I've done more testing. LockRow does work after all!