Restrict delete of UD Table row on form

This is my first time working with a UD table and this thread was a big help. I added a UD table to OrderHed and change the menu item from the cryptic “New UDxx” wording.

Now I need to restrict deleting a row if the row’s CheckBox01 is set to true. The wizard added code to trigger delete is below but I’m having trouble accessing the row in question to verify the checkbox value. I tried a if statement using the drsDeleted DateRow the code creates but keep getting errors.

How do I determine which row the user has select on the grid?
How do I then check the field value?

	private void SalesOrderForm_AfterToolClickForUD28(object sender, Ice.Lib.Framework.AfterToolClickEventArgs args)
	{
		// EpiMessageBox.Show(args.Tool.Key);
		switch (args.Tool.Key)
		{
			case "DeleteTool":
				if ((args.Cancelled == false))
				{
					DeleteUD28Record();
				}
				break;
		}
	}

	private void DeleteUD28Record()
	{
		// Check to see if deleted view is ancestor view
		bool isAncestorView = false;
		Ice.Lib.Framework.EpiDataView parView = this._edvUD28.ParentView;

		while ((parView != null))
		{
			if ((this.oTrans.LastView == parView))
			{
				isAncestorView = true;
				break;
			} else
			{
				parView = parView.ParentView;
			}
		}

		// If Ancestor View then delete all child rows
		if (isAncestorView)
		{
			DataRow[] drsDeleted = this._ud28Adapter.UD28Data.UD28.Select("Key1 = \'" + this._Key1UD28 + "\' AND Key2 = \'" + this._Key2UD28 + "\' AND Key3 = \'" + this._Key3UD28 + "\' AND Key4 = \'" + this._Key4UD28 + "\'");
			for (int i = 0; (i < drsDeleted.Length); i = (i + 1))
			{
				this._ud28Adapter.Delete(drsDeleted[i]);
			}
		} else
		{
			if ((this.oTrans.LastView == this._edvUD28))
			{
				if ((this._edvUD28.Row >= 0))
				{
					DataRow drDeleted = ((DataRow)(this._ud28Adapter.UD28Data.UD28.Rows[this._edvUD28.Row]));
					if ((drDeleted != null))
					{
						if (this._ud28Adapter.Delete(drDeleted))
						{
							if ((_edvUD28.Row > 0))
							{
								_edvUD28.Row = (_edvUD28.Row - 1);
							}

							// Notify that data was updated.
							this._edvUD28.Notify(new EpiNotifyArgs(this.oTrans, this._edvUD28.Row, this._edvUD28.Column));
						}
					}
				}
			}
		}
	}

@Randy i have some code posted Epicor Insights 2019 Customization Tips Tricks and Troubleshooting Code about getting selected rows in a grid…

1 Like

I seem to be missing something, I used your “btnSelectHighlightedPacks_Click” and “GatherRows” functions as the others didn’t seem to apply for my needs but I’m getting an errors:

 Error: CS1928 - line 960 (6057) - 'Infragistics.Win.UltraWinGrid.SelectedRowsCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments
 Error: CS1929 - line 960 (6057) - Instance argument: cannot convert from 'Infragistics.Win.UltraWinGrid.SelectedRowsCollection' to 'System.Data.EnumerableRowCollection'
using System.Collections.Generic;

public class Script
{
    EpiUltraGrid pcoGrid;
    List<UltraGridRow> gatherRows;
}

public void InitializeCustomCode()
{
    pcoGrid = (EpiUltraGrid)csm.GetNativeControlReference("ab1a9460-3e4a-4ab9-8450-8221ad0aac01");
    gatherRows = new List<UltraGridRow>();
}

private void btnVerifyCheckBox01_Click(object sender, System.EventArgs args)
{
    List<UltraGridRow> gridRows = (from UltraGridRow r in pcoGrid.Selected.Rows
                                   where Convert.ToBoolean(r.Cells["CheckBox01"].Value).Equals(true)
                                   && r.GetType() != typeof(UltraGridGroupByRow)
                                   select r).ToList();
}

private void GatherRows(RowsCollection row, string field, bool val)
{
    List<UltraGridRow> gridRows = new List<UltraGridRow>();
    foreach (UltraGridRow r in row)
    {
        if (r.GetType() == typeof(UltraGridGroupByRow))
        {
            // A recursive query that gets to the bottom of things
            GatherRows(((UltraGridGroupByRow)r).Rows, field, val);
        }
        else
        {
            if (r.Cells[field].Value.Equals(val))
            {
                // Add this to our collection of stuff to do
                gatherRows.Add(r);
            }
        }
    }
}

After beating my head against the wall a little, I got the select code working, it’s a bit different.

	private void btnDeleteSelected_Click(object sender, System.EventArgs args)
	{
		SelectedRowsCollection selectedRowsCollection = pcoGrid.Selected.Rows;
		foreach (UltraGridRow selectedRow in selectedRowsCollection)
		{
			if ( selectedRow.Cells["CheckBox01"].Value.Equals(true) )
			{
				EpiMessageBox.Show("This line has already been Submitted and can not be deleted.");   // !No puede borrar!
			}
			else
			{
				DeleteUD28Record();  //Call wizard made delete record
			}
		}

Rob,

Thanks! Your code helped a ton.