I'm building this at run time, based on things like the name of the entityfield displayed in the column of a grid. This info is pretty easily available. The Entity.FieldIndex enum value isn't since it could be any entity and there is not one big enum that contains all fields (that I know of).
Or am I missing a better way to do this kind of thing?
private void gridInvoices_FilterChange(object sender, EventArgs e) {
C1TrueDBGrid grid = sender as C1TrueDBGrid;
if(grid != null) {
// build our filter expression
PredicateExpression filter = new PredicateExpression();
foreach(C1DataColumn dc in grid.Columns) {
if(dc.FilterText.Length > 0) {
//TODO: replace hardcoded entity name with variable!
IEntityField2 field = EntityFieldFactory.Create("InvoiceEntity", dc.Caption);
IPredicate pred;
switch(field.DataType.Name) {
case "String":
pred = new FieldLikePredicate(field, null, dc.FilterText);
break;
case "Int32":
pred = new FieldCompareValuePredicate(field, null, ComparisonOperator.Equal, dc.FilterText);
break;
case "DateTime":
DateTime work;
if (DateTime.TryParse(dc.FilterText, out work)) {
pred = new FieldCompareValuePredicate(field, null, ComparisonOperator.Equal, work);
}
break;
default:
Debug.Assert(false,"Deal with data type");
pred = new FieldCompareValuePredicate(field, null, ComparisonOperator.Equal, dc.FilterText);
break;
}
if( pred != null) {
filter.AddWithAnd(pred);
}
}
}
// filter the data
EntityView2<InvoiceEntity> vw = grid.DataSource as EntityView2<InvoiceEntity>;
if (filter.Count > 0)
{
try
{
vw.Filter = filter;
}
catch(ArgumentOutOfRangeException except) {
Debug.Write(except.Message);
}
} else {
vw.Filter = null;
}
}
}