Hi,
We are using the ComponentArt Grid in a project (I read that you don't fancy this component that much, Frans ) and all is working fine except for the sorting. When the user presses the column header to sort a column, the grid's Sort property is filled with the SQL sort expression needed to do that sort (like "Description ASC") and I am trying to convert that into an LLBLGen SortExpression. And of course I want to make that as generic as possible. Below is the code I have so far. This works fine, if the column that is used in the grid is from the same entity type that I have passed to the GetSortExpression function, but I can't figure out how to do the same if the column is from a 'joined entity'. The sqlSortExpression then is something like "CreatedByEmployee.FirstLastName ASC". So, in that case I first have to figure out what entity type CreatedByEmployee is and then set the current entity variable to that type.
Now my question is: is that possible?
I have set the entity variable to the correct entity type manually (new EmployeeEntity()) and then the privided SQL sort expression works fine too.
Anyway, hope I am being clear.
/// <summary>
/// Returns an LLBLGen SortExpression based on a SQL sort string (sorted on 1 field)
/// </summary>
/// <param name="entity">Example: QuestionnaireEntity</param>
/// <param name="sqlSortExpression">Example: Description ASC</param>
/// <returns>The LLBLGen SortExpression</returns>
public static SortExpression GetSortExpression(EntityBase2 entity, string sqlSortExpression)
{
SortExpression sortExpression = new SortExpression();
if (!string.IsNullOrEmpty(sqlSortExpression))
{
Array sqlSortExpressionArray = sqlSortExpression.Split(Char.Parse(" "));
// Set the sort order
string sortOrder = string.Empty;
sortOrder = sqlSortExpressionArray.GetValue(1).ToString();
SortOperator sortOperator = new SortOperator();
if (sortOrder.Equals("ASC"))
{
sortOperator = SortOperator.Ascending;
}
else
{
sortOperator = SortOperator.Descending;
}
// Set the sort field
string sortField = string.Empty;
sortField = sqlSortExpressionArray.GetValue(0).ToString();
// ------------ not generic yet --------------
if (sortField.Contains("."))
{
// CreatedByEmployee.FirstLastName ASC
Array sortFieldArray = sortField.Split(Char.Parse("."));
// Walk through the entities
sortField = sortFieldArray.GetValue(sortFieldArray.Length-1).ToString();
for (int i = 0; i < sortFieldArray.Length-1; i++)
{
string fieldName = sortFieldArray.GetValue(i).ToString();
//foreach (EntityBase2 tempEntity in EntityClasses)
//{
// try
// {
// entity.SetRelatedEntity(entity, fieldName);
// }
// catch (exception e)
// {
// }
//}
entity = new EmployeeEntity();
}
}
// ------------ not generic yet --------------
SortClause sortClause = new SortClause(GetEntityField(sortField, entity.Fields), null, sortOperator);
sortExpression.Add(sortClause);
}
return sortExpression;
}
private static IEntityField2 GetEntityField(string fieldName, IEntityFields2 fields)
{
IEntityField2 entityField2 = null;
foreach (IEntityField2 tempEntityField2 in fields)
{
if (tempEntityField2.Name.Equals(fieldName))
{
entityField2 = tempEntityField2;
break;
}
}
return entityField2;
}
I am using LLBLGen Pro 2 final by the way (with lots of joy).