locked
Filter for Non-FK fields? RRS feed

  • Question

  • User-116525033 posted

     Hi,

     

    is it possible to define  a dropdown or autocomplete filter for non-foreignkey fields?

     

    Have a nice day,

    Alex

    Monday, August 25, 2008 10:10 AM

Answers

  • User-330204900 posted

    is it possible to define  a dropdown or autocomplete filter for non-foreignkey fields?

    Yes it is you will need Dynamic Data Futures 7/16 and this is a modified version of the Integer filter in there what it does is get a Distinct list from the selected column and return it in the Dropdown list works OK.

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Web.DynamicData;
    using Microsoft.Web.DynamicData;
    
    namespace DynamicDataFuturesSample
    {
    	public partial class LimitList_Filter : FilterUserControlBase, ISelectionChangedAware
    	{
    
    		public event EventHandler SelectionChanged
    		{
    			add
    			{
    				DropDownList1.SelectedIndexChanged += value;
    			}
    			remove
    			{
    				DropDownList1.SelectedIndexChanged -= value;
    			}
    		}
    
    		protected void Page_Init(object sender, EventArgs e)
    		{
    			var items = Column.Table.GetQuery();
    
    			// row
    			var entityParam = Expression.Parameter(Column.Table.EntityType, "row");
    			// row => row.Property
    			var columnLambda = Expression.Lambda(Expression.Property(entityParam, Column.EntityTypeProperty), entityParam);
    			// Items.Select(row => row.Property)
    			var selectCall = Expression.Call(typeof(Queryable), "Select", new Type[] { items.ElementType, columnLambda.Body.Type }, items.Expression, columnLambda);
    			// Items.Select(row => row.Property).Distinct
    			//var distinctCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);
    			var whereCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);
    
    			//var result = items.Provider.CreateQuery(distinctCall);
    			var result = items.Provider.CreateQuery(whereCall);
    
    			foreach (var item in result)
    			{
    if(item != null) // drop any null items ***UPDATED*** DropDownList1.Items.Add(item.ToString()); } } public override string SelectedValue { get { return DropDownList1.SelectedValue; } } } }

    Hope this helps [:D]

    P.S. I'll do a detailed post on my blog when I get home early September [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 25, 2008 7:07 PM

All replies

  • User-330204900 posted

    is it possible to define  a dropdown or autocomplete filter for non-foreignkey fields?

    Yes it is you will need Dynamic Data Futures 7/16 and this is a modified version of the Integer filter in there what it does is get a Distinct list from the selected column and return it in the Dropdown list works OK.

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Web.DynamicData;
    using Microsoft.Web.DynamicData;
    
    namespace DynamicDataFuturesSample
    {
    	public partial class LimitList_Filter : FilterUserControlBase, ISelectionChangedAware
    	{
    
    		public event EventHandler SelectionChanged
    		{
    			add
    			{
    				DropDownList1.SelectedIndexChanged += value;
    			}
    			remove
    			{
    				DropDownList1.SelectedIndexChanged -= value;
    			}
    		}
    
    		protected void Page_Init(object sender, EventArgs e)
    		{
    			var items = Column.Table.GetQuery();
    
    			// row
    			var entityParam = Expression.Parameter(Column.Table.EntityType, "row");
    			// row => row.Property
    			var columnLambda = Expression.Lambda(Expression.Property(entityParam, Column.EntityTypeProperty), entityParam);
    			// Items.Select(row => row.Property)
    			var selectCall = Expression.Call(typeof(Queryable), "Select", new Type[] { items.ElementType, columnLambda.Body.Type }, items.Expression, columnLambda);
    			// Items.Select(row => row.Property).Distinct
    			//var distinctCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);
    			var whereCall = Expression.Call(typeof(Queryable), "Distinct", new Type[] { Column.EntityTypeProperty.PropertyType }, selectCall);
    
    			//var result = items.Provider.CreateQuery(distinctCall);
    			var result = items.Provider.CreateQuery(whereCall);
    
    			foreach (var item in result)
    			{
    if(item != null) // drop any null items ***UPDATED*** DropDownList1.Items.Add(item.ToString()); } } public override string SelectedValue { get { return DropDownList1.SelectedValue; } } } }

    Hope this helps [:D]

    P.S. I'll do a detailed post on my blog when I get home early September [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 25, 2008 7:07 PM
  • User1024101778 posted

    Check out Dynamic Data Filtering.  It supports filtering on any type of property and works with the current version of Dynamic Data.

    http://www.codeplex.com/DynamicDataFiltering 

    Start with the Overview.doc and then the AdventureWorks Sample under releases.
     

    Tuesday, August 26, 2008 1:21 AM
  • User-116525033 posted

    @Steve: Great! simple but exactly what I was searching for! Thanks a lot!

    @jheyse: thank you for your reply also. I had a look at the filtering project and saw the stuff with the static parameter which resolves another problem of mine, I think. Moreover I'll need the search capabilities in the future, I'm sure :-)

    Wednesday, August 27, 2008 10:34 AM
  • User-2013179010 posted

    That's a nice fix for a simple distinct list filter - anyone know of one which gets its list from another db table? It would be fairly trivial to make I guess, but I figure it might exist already. I need FK field functionality but I don't want to have to do a bajillion joins to get my records.

    Wednesday, August 27, 2008 3:41 PM
  • User-330204900 posted

    Hi if it's just another column than the default you get with FK field then you can use DisplayColumn

    [DisplayColumn("FullName", "FullName", true)]

    The first parameter is Column to display

    The second is the column to sort on

    And the third is the false for sort ascending and true for sort descending.

    Hope this helps [:D]

    Wednesday, August 27, 2008 6:13 PM