Populate Drop Down in Custom Drop Down

I have added a Drop Down to Job Entry and tied it to an custom field I added in. Now I am wanting to populate a few tings in this drop down to be selected. How do you do that?

There are a few ways to do it. Are the items static or are they in a database table.

static list:
dropdown.items.Add(ā€œitem1ā€);
dropdown.items.Add(ā€œitem2ā€);
dropdown.items.Add(ā€œitem3ā€);

database and the value is different then text:
dsUsers = fillFromDB();

dropdown.DataSource = dsUsers ;
dropdown.DisplayMember = ā€œUserNameā€;
dropdown.ValueMember = ā€œUserIDā€;

Static. This part I am not very knowledgeable, so I am assuming this would be placed in Script Editor, correct?

Yes. in the script editor.

Wellā€¦This form here is quite customized already. Can you give me an idea on where to put it at? Here is a copy of our Script Editor right now.

extern alias Erp_Contracts_BO_Project;
extern alias Erp_Contracts_BO_Part;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.UI;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using Erp.Adapters;
using System.Collections.Generic;

public class Script
{
// ** Wizard Insert Location - Do Not Remove ā€˜Begin/End Wizard Added Module Level Variablesā€™ Comments! **
// Begin Wizard Added Module Level Variables **

private EpiBaseAdapter oTrans_jobAdapter;
private DataView JobHead_DataView;
// End Wizard Added Module Level Variables **

// Add Custom Module Level Variables Here **

private PartAdapter partAdapter;
private EpiDataView edvJobHead;

private bool getRevisionButtonClicked = false;

public void InitializeCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
	// Begin Wizard Added Variable Initialization

	this.JobHead_Column.ColumnChanged += new DataColumnChangeEventHandler(this.JobHead_AfterFieldChange);
	this.oTrans_jobAdapter = ((EpiBaseAdapter)(this.csm.TransAdaptersHT["oTrans_jobAdapter"]));
	this.JobHead_DataView = this.JobHead_Row.dataView;
	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls

	this.GetRevisionButton.Click += new System.EventHandler(this.GetRevisionButton_Click);
	this.btnGetSizesShapes.Click += new System.EventHandler(this.btnGetSizesShapes_Click);
	// End Wizard Added Custom Method Calls

	edvJobHead = (EpiDataView)oTrans.EpiDataViews["JobHead"];
}

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

	this.GetRevisionButton.Click -= new System.EventHandler(this.GetRevisionButton_Click);
	this.JobHead_Column.ColumnChanged -= new DataColumnChangeEventHandler(this.JobHead_AfterFieldChange);
	this.oTrans_jobAdapter = null;
	this.JobHead_DataView = null;
	this.btnGetSizesShapes.Click -= new System.EventHandler(this.btnGetSizesShapes_Click);
	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	// End Custom Code Disposal
}

private void GetRevisionButton_Click(object sender, System.EventArgs args)
{
	// ** Place Event Handling Code Here **

	getRevisionButtonClicked = true;

	/* Check for required values */
	if (edvJobHead.Row < 0 || string.IsNullOrEmpty(edvJobHead.dataView[edvJobHead.Row]["Size_c"].ToString()) ||
	    string.IsNullOrEmpty(edvJobHead.dataView[edvJobHead.Row]["Shape_c"].ToString()) ||
	    string.IsNullOrEmpty(edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString()) ||
		decimal.Parse(edvJobHead.dataView[edvJobHead.Row]["ProdQty"].ToString()) == 0)
	{
		MessageBox.Show("Error: Part, Size, Shape, and Prod Qty must be entered");
	}
	else
	{
		/* Get values from the form */
		string partNum = edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString();
		string size = edvJobHead.dataView[edvJobHead.Row]["Size_c"].ToString();
		string shape = edvJobHead.dataView[edvJobHead.Row]["Shape_c"].ToString();
		decimal lineQty = decimal.Parse(edvJobHead.dataView[edvJobHead.Row]["ProdQty"].ToString());

		/* Get the Part and its revisions */
		partAdapter = new PartAdapter(oTrans);
		partAdapter.BOConnect();
		partAdapter.GetByID(partNum);
		if (partAdapter.PartData.PartRev.Count <= 0)
		{
			MessageBox.Show("Error: No revisions found");
		}
		else
		{	

			/* We are looking for the Revision with the highest MinQty that is not greater than the line Qty.
			MinQty must be <= Line Qty.  That min is stored here */
			decimal bestMin = -1;

			/* The index of the RevNum in the PartData.PartRev dataset that corresponds to the bestMin */
			int index = -1;

			/* Loop through the Part Revisions */
			for (int i = 0; i < partAdapter.PartData.PartRev.Count; i++)
			{
			 	if (!string.IsNullOrEmpty(partAdapter.PartData.PartRev[i]["Size_c"].ToString()) &&
					 !string.IsNullOrEmpty(partAdapter.PartData.PartRev[i]["Shape_c"].ToString()))
				 {
					string revSize = partAdapter.PartData.PartRev[i]["Size_c"].ToString();
					string revShape = partAdapter.PartData.PartRev[i]["Shape_c"].ToString();
					//MessageBox.Show("Found Rev with size" + revSize + " and shape " + revShape);

					if (revSize == size && revShape == shape)
					{
						/* The Shape and Size on the revision match the values on the form */
						//MessageBox.Show("Size and Shape on Quote line match revision");
						int revMinQty;
						if (int.TryParse(partAdapter.PartData.PartRev[i]["MinQty_c"].ToString(), out revMinQty))
						{
							/* MinQty on the Revision is not blank */ 
							//MessageBox.Show("PartRev has min qty of " + revMinQty);
							if (revMinQty <= lineQty && revMinQty > bestMin)
							{
								/* We have found a revision that A. Has a min qty not greater than line quantity and
								B. Has a min qty that is greater than the default min qty or another suitable min qty found */
								//MessageBox.Show("Old highest min: " + bestMin + ", new highest min: " + revMinQty);
								bestMin = revMinQty;
								index = i;
							}	
						}
					}
				}
			}
			if (bestMin > -1 && index > -1)
			{
				/* We have found the best PartRev.  Set it on the form */
				string bestRevNum = partAdapter.PartData.PartRev[index]["RevisionNum"].ToString();
				//MessageBox.Show("The best Revision is " + bestRevNum);
				edvJobHead.dataView[edvJobHead.Row]["RevisionNum"] = bestRevNum;		
			}	
			else
			{
				MessageBox.Show("Error: No Suitable Revision was Found");
				edvJobHead.dataView[edvJobHead.Row]["RevisionNum"] = string.Empty;
			}
		}
	}
	getRevisionButtonClicked = false;
}

private void SearchOnUD01AdapterFillDropDown()
{
	// Wizard Generated Search Method
	// You will need to call this method from another method in custom code
	// For example, [Form]_Load or [Button]_Click

	bool recSelected;
	string whereClause = string.Empty;
	System.Data.DataSet dsUD01Adapter = Ice.UI.FormFunctions.SearchFunctions.listLookup(this.oTrans, "UD01Adapter", out recSelected, false, whereClause);
	if (recSelected)
	{
		// Set EpiUltraCombo Properties
		this.ShapeDropdown.ValueMember = "Key1";
		this.ShapeDropdown.DataSource = dsUD01Adapter;
		this.ShapeDropdown.DisplayMember = "Key2";
		string[] fields = new string[] {
				"Key2"};
		this.ShapeDropdown.SetColumnFilter(fields);
	}
}

private void JobEntryForm_Load(object sender, EventArgs args)
{
	// Add Event Handler Code
	SearchOnUD01AdapterFillDropDown();
}

private void JobHead_AfterFieldChange(object sender, DataColumnChangeEventArgs args)
{
	// ** Argument Properties and Uses **
	// args.Row["FieldName"]
	// args.Column, args.ProposedValue, args.Row
	// Add Event Handler Code
	switch (args.Column.ColumnName)
	{
		case "PartNum":

			/* Discard the existing datasouce for the Size dropdown and blank out its value */
			this.SizeDropdown.DataSource = null;
			edvJobHead.dataView[edvJobHead.Row]["Size_c"] = string.Empty;

			/* Repopulate the Size dropdown with data from part revisions */
			string newPartNum = edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString();
			SetSizeDropdownOptions(newPartNum);

			/* Set the Size and Shape equal to those on the default revision */
			GetSizeAndShapeFromRevision();

			break;
		case "RevisionNum":
			if (getRevisionButtonClicked)
			{
				//MessageBox.Show("Rev Changed: Button clicked, returning");
				return;
			}
			else
			{
				//MessageBox.Show("Rev Changed: Manual Selection, getting size and shape");
				/* Revision was manually changed: Set the Size and Shape equal to those on the revision */
				GetSizeAndShapeFromRevision();
			}
			break;
	}
}

/* Lookup the RevisionNum on the entered Part and get default Size and Shape */
private void GetSizeAndShapeFromRevision()
{
	string partNum = edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString();
	string revisionNum = edvJobHead.dataView[edvJobHead.Row]["RevisionNum"].ToString();

	/* Get the Part and its revisions */
	partAdapter = new PartAdapter(oTrans);
	partAdapter.BOConnect();
	bool getByIDSuccess = partAdapter.GetByID(partNum);
	if (getByIDSuccess)
	{
		int partRevIndex = -1;
		/* Find the index of the PartRev with the RevisionNum we are looking for */
		for (int i = 0; i < partAdapter.PartData.PartRev.Count; i++)
		{
			if (partAdapter.PartData.PartRev[i]["RevisionNum"].ToString() == revisionNum)
			{
				partRevIndex = i;
				break;
			}
		}
		if (partRevIndex > -1)
		{
			/* Set the Size and Shape equal to those on the Part Revision */
			edvJobHead.dataView[edvJobHead.Row]["Size_c"] = partAdapter.PartData.PartRev[partRevIndex]["Size_c"];
			edvJobHead.dataView[edvJobHead.Row]["Shape_c"] = partAdapter.PartData.PartRev[partRevIndex]["Shape_c"];
		}			
	}
}

/* Populate the size dropdown based on entered part. */
private void SetSizeDropdownOptions(string newPartNum)
{
	//Find possible sizes for this part 
	partAdapter = new PartAdapter(oTrans);
	partAdapter.BOConnect();
	
	/* Lookup the Part and its Revisions */
	bool canFindPart = partAdapter.GetByID(newPartNum);
	if (canFindPart && partAdapter.PartData != null && partAdapter.PartData.PartRev != null && partAdapter.PartData.PartRev.Count > 0)
	{
		
		/* A list to hold unique size values */
		List<string> uniqueSizes = new List<string>();
	
		/* Loop through the revisions looking for revisions that are approved
			and have a size that is not null and not already in our list of unique sizes */
		for (int i = 0; i < partAdapter.PartData.PartRev.Count; i++)
		{
			if (partAdapter.PartData.PartRev[i]["Size_c"] != string.Empty &&
			!uniqueSizes.Contains(partAdapter.PartData.PartRev[i]["Size_c"].ToString()) &&
			bool.Parse(partAdapter.PartData.PartRev[i]["Approved"].ToString()) == true)
			{
				/* PartRev matches our criteria, so add it to list */
				uniqueSizes.Add(partAdapter.PartData.PartRev[i]["Size_c"].ToString());
			}
		}
		/* Create a DataTable which will be the data source for the Size dropdown.
		Add a new column "Size_c" and add each unique size from the list to a new row */
		DataTable dt = new DataTable();
		dt.Columns.Add("Size_c");
		foreach (string size in uniqueSizes) 
		{
			dt.Rows.Add(size);
		}
		this.SizeDropdown.DataSource = dt;
		this.SizeDropdown.ValueMember = "Size_c";
		this.SizeDropdown.DisplayMember = "Size_c"; 
	}
}

private void btnGetSizesShapes_Click(object sender, System.EventArgs args)
{
	// ** Place Event Handling Code Here **
			// ** Place Event Handling Code Here **
					/* Discard the existing datasouce for the Size dropdown and blank out its value */
	if (edvJobHead.Row > -1 && edvJobHead.dataView[edvJobHead.Row] != null && edvJobHead.dataView[edvJobHead.Row]["PartNum"] != null
	   && !string.IsNullOrEmpty(edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString()))
	{

		this.SizeDropdown.DataSource = null;
		edvJobHead.dataView[edvJobHead.Row]["Size_c"] = string.Empty;

		/* Repopulate the Size dropdown with data from part revisions */
		string newPartNum = edvJobHead.dataView[edvJobHead.Row]["PartNum"].ToString();
		SetSizeDropdownOptions(newPartNum);

		/* Set the Size and Shape equal to those on the default revision */
		GetSizeAndShapeFromRevision();
	}
}

}

Blockquote

Just create a method.

public void LoadDropdown()
{
dropdown.items.add(ā€œoneā€);
dropdown.items.add(ā€œtwoā€);
dropdown.items.add(ā€œthreeā€);
}

and call it in the
private void JobEntryForm_Load(object sender, EventArgs args)
{
// Add Event Handler Code
SearchOnUD01AdapterFillDropDown();
LoadDropdown(); <--------------
}

Add exactly as you have, except the obvious thats not needed, adn got he follwoing error: Compiling Custom Code ā€¦

----------errors and warnings------------

Error: CS0111 - line 195 (1193) - Type ā€˜Scriptā€™ already defines a member called ā€˜JobEntryForm_Loadā€™ with the same parameter types

** Compile Failed. **

I am guessing I just need to change the name a little bit to make this work.

This is an example of a combo box with 3 options and different display and value text.

The combo displays Frameless, Framed and both, but will save FL,F and ā€œā€

Normally I would use a user code since then a user can right click on the field to add new values.

Hereā€™s an old video I did on combo boxes. It has user codes in it.

2 Likes

Your code already has the JobEntryFormLoad method. Just add the LoadDropdown() call to it.

1 Like

After reviewing your screenshots and watching a portion of the video, in Extended Properties, I have this set to string instead of Character. What i get for early morning work! Anyway, I am assuming to change that I need to remove, regen, and do it all over again. Correct?

You shouldnā€™t need to do any sort of a regen. Updates to extended properties take effect after restarting the client. The video was from 9 so that is probably the string vs character difference.

The Data Type is string and it is greyed out. How do I change it without removing it and starting over?

Why do you need to change the data type? To set a user code all you should need to update is the UD Code Type.

Ok, my assumption. But, when I am trying to get this drop down box to work tied to this field, the EpiCombo options for me are only DescColumnName and EpiSystemCode. It doesnā€™t give me all the other options listed in the pic above. Was thinking that was connected to the String and Not Character and wanted to make sure everything was correct before I try to populate the drop down.

Just add this method to your existing code base

private static void FillDropDown()
{
DataTable dt = new DataTable();
dt.Columns.Add(ā€œNameā€);

DataRow row = dt.NewRow();
row[0]="item1";		
dt.Rows.Add(row);

row = dt.NewRow();
row[0]="item2";		
dt.Rows.Add(row);

   DataSet ds = new DataSet();
   ds.Tables.Add(dt);

   epiComboC1.DataSource = ds;

}

And call it on your existing form load event (JobEntryForm_Load)

FillDropDown();

1 Like

@dpenn I FINALLY can get back to this! I did everything as you said, just have one more question before I put this in. I see Name, Item1 and Item2. I know what the items are, but what is the Name supposed to represent?

ā€˜Nameā€™ is just the Column name of the DataTable. You can make it anything.

Iā€™m assuming, since its in parenthesis, i do not need the quotes, correct?

No. You do need the Quotes because the column name is a string. ā€œNameā€

never mind, that was dumb