NamedLobSystemInstanceDictionary sysInstances = ApplicationRegistry.GetLobSystemInstances();
LobSystemInstance myIns = sysInstances[Request[“BDCApplication”]];
Entity myEntity = myIns.GetEntities()[Request[“BDCEntity”]];
//Build up a finder collection which is the query for the BDC source.
FilterCollection fc = myEntity.GetFinderFilters();
for (int iCounter = 0; iCounter < fc.Count; iCounter++)
{
if (fc[iCounter].Name == sFinderName || sFinderName == "")
{
switch (fc[iCounter].GetType().FullName)
{
case "Microsoft.Office.Server.ApplicationRegistry.Runtime.WildcardFilter":
((WildcardFilter)fc[iCounter]).SystemIndependentValue = "*" + sQueryString + "*";
break;
case "Microsoft.Office.Server.ApplicationRegistry.Runtime.ComparisonFilter":
((ComparisonFilter)fc[iCounter]).Value = Convert.ChangeType(sQueryString, ((ComparisonFilter)fc[iCounter]).GetFilterValueType());
break;
case "Microsoft.Office.Server.ApplicationRegistry.Runtime.LimitFilter":
((LimitFilter)fc[iCounter]).Value = Math.Min(0xc9, ((LimitFilter)fc[iCounter]).MaximumValue);
break;
}
}
}
IEntityInstanceEnumerator prodEntityInstanceEnumerator = myEntity.FindFiltered(fc, myIns);
//Create a data table to store the results.
DataTable Results = new DataTable("BDCData");
Resultset.Tables.Add(Results);
//Get the collection of IDs and create an array to store their names.
IdentifierCollection EntIDS = myEntity.GetIdentifiers();
string[] sIDNames = new string[EntIDS.Count];
//Get the names.
int nCounter = 0;
foreach (Identifier currentID in EntIDS)
{
sIDNames[nCounter] = currentID.Name;
nCounter++;
}
//Add columns to the results data table named as per the Entities fields.
//Check if there any of the fields have show in picker set so we know to display only those fields.
Boolean blnShowInPickers = false;
foreach (Field f in myEntity.GetFinderView().Fields)
{
if (f.TypeDescriptor.GetProperties().ContainsKey("ShowInPicker"))
{
if (((Boolean)f.TypeDescriptor.GetProperties()["ShowInPicker"]))
{
blnShowInPickers = true;
}
}
}
DataColumn currentColumn = null;
foreach (Field f in myEntity.GetFinderView().Fields)
{
//Add the default display name to the caption if there is one so we can pull it out when we create the list.
if (f.DefaultDisplayName == "")
{
currentColumn = Results.Columns.Add(f.Name);
}
else
{
currentColumn = Results.Columns.Add(f.Name);
currentColumn.Caption = f.DefaultDisplayName;
}
if (blnShowInPickers)
{
//Only show fields that have show in picker set (if any do).
if (f.Name != sTitle)
{
if (f.TypeDescriptor.GetProperties().ContainsKey("ShowInPicker"))
{
if (((Boolean)f.TypeDescriptor.GetProperties()["ShowInPicker"]))
{
currentColumn.ExtendedProperties.Add("ShowInPicker", "TRUE");
}
else
{
currentColumn.ExtendedProperties.Add("ShowInPicker", "FALSE");
}
}
}
else
{
currentColumn.ExtendedProperties.Add("ShowInPicker", "TRUE");
}
}
else
{
//Else if the field is an identifier or it’s the title field then show it.
if (f.TypeDescriptor.ContainsIdentifier || f.Name == sTitle)
{
currentColumn.ExtendedProperties.Add("ShowInPicker", "TRUE");
}
else
{
currentColumn.ExtendedProperties.Add("ShowInPicker", "FALSE");
}
}
}
//Loop through the found Entity instances and add them to the results.
while (prodEntityInstanceEnumerator.MoveNext())
{
try
{
IEntityInstance IE = prodEntityInstanceEnumerator.Current;
DataRow newResultRow = Results.NewRow();
System.Collections.IList MyList = (System.Collections.IList)IE.GetIdentifierValues();
//Create an array to hold the identifiers.
object[] oIdentitiers = new object[sIDNames.Length];
for (int iCounter = 0; iCounter < MyList.Count; iCounter++)
{
oIdentitiers[iCounter] = MyList[iCounter].ToString();
}
foreach (Field f in myEntity.GetFinderView().Fields)
{
if (IE[f] != null)
{
newResultRow[f.Name] = IE[f];
}
}
Results.Rows.Add(newResultRow);
}
catch (Exception rowex)
{
}
}
//Grab the Display Fields and add columns for them in the Grid View.
gvResults.DataSource = Resultset;
string[] keys = Request["DisplayFields"].ToUpper().Split(',');
gvResults.DataKeyNames = keys;
gvResults.Columns.Clear();
foreach (DataColumn currentCol in ((DataTable)gvResults.DataSource).Columns)
{
string[] displayFields = Request["DisplayFields"].ToUpper().Split(',');
if (Array.BinarySearch(displayFields,currentCol.ColumnName.ToString().ToUpper())>=0)
{
BoundField newfield=new BoundField();
newfield.HeaderText=currentCol.ColumnName.ToString();
newfield.DataField=currentCol.ColumnName.ToString();
gvResults.Columns.Add(newfield);
}
}
//Bind the results to the grid.
gvResults.DataBind();