This is a migrated thread and some comments may be shown as answers.

Evaluate "UniqueName" in Custom Column Filter class

4 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gabe
Top achievements
Rank 1
Gabe asked on 07 Feb 2013, 05:43 PM
I've been working through the "MyCustomFilteringColumn" overrides code example, and I've got everything working except I can't seem to get the filter to work, unless I hard code the column name in my query. I'm working with a Data Template Object, and populating the filter dropdown list with Linq query results. But I can't figure out how to evaluate the UniqueName of the passing object (this.UniqueName) as a parameter of my Data Object

Here's what I have in my code:
private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            ((RadComboBox)o).DataTextField = this.DataField;
            ((RadComboBox)o).DataValueField = this.DataField;
 
            var employees = from emp in EmployeeBL.GetAllEmployees()
                            where emp.FullName.ToLower().Contains(e.Text.ToLower())
                            select emp;
            ((RadComboBox)o).DataSource = employees;
 
            ((RadComboBox)o).DataBind();
        }

I've hardcoded "FullName" which is a property of EmployeeDTO (GetAllEmployees() returns a List<EmployeDTO>), but what I'd like to do is have this.UniqueName evaluate to "FullName" or whatever column is requesting the list.

Thanks,
Gabe

4 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 12 Feb 2013, 11:28 AM
Hi Gabe,

You can try extracting the name of the column from the grid cell like shown in the code snippet below:
private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
   {
       string ColumnName=(((RadComboBox)o).Parent as GridTableCell).Column.UniqueName;
       ((RadComboBox)o).DataTextField = this.DataField;
       ((RadComboBox)o).DataValueField = this.DataField;
 
       var employees = from emp in EmployeeBL.GetAllEmployees()
                       where emp.FullName.ToLower().Contains(e.Text.ToLower())
                       select emp;
       ((RadComboBox)o).DataSource = employees;
 
       ((RadComboBox)o).DataBind();
   }

Kind regards,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Gabe
Top achievements
Rank 1
answered on 12 Feb 2013, 02:10 PM
I don't think I was clear in my post.  I don't think I have an issue with finding the column name, but where I'm hitting a block is in passing that column name into the WHERE clause.

var employees = from emp in EmployeeBL.GetAllEmployees()
                            where emp.FullName.ToLower().Contains(e.Text.ToLower())
                            select emp;

The above code works because "FullName" is a property of emp (which is returned as a List<EmployeeDTO>).  What I'm trying to do (unsuccessfully) is to evaluate whatever is passed into this function as "this.UniqueName" (or ColumnName in your example) so that the WHERE clause says "look in 'this.UniqueName' column and return matching values" so that if the "FullName" column filter box is sending the request, the results compare to the FullName column.  But if the "Description" column filter box sends the request, this.UniqueName evaluates to "Description" and results are drawn from the "Description" column in the resultset.

I had tried this, previously:

var employees = from emp in EmployeeBL.GetAllEmployees()
     where emp.(this.UniqueName).ToLower().Contains(e.Text.ToLower())
     select emp;


I hope that is clearer?

Thanks.
0
Accepted
Angel Petrov
Telerik team
answered on 14 Feb 2013, 04:06 PM
Hello Gabe,

If I understand correctly the requirements you can use something like this:
private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
   {
       string ColumnName=(((RadComboBox)o).Parent as GridTableCell).Column.UniqueName;
       ((RadComboBox)o).DataTextField = this.DataField;
       ((RadComboBox)o).DataValueField = this.DataField;
  
       var employees;
       switch (ColumnName)
        {
            case "FullName":
                employees = from emp in EmployeeBL.GetAllEmployees()
                where emp.FullName.ToLower().Contains(e.Text.ToLower())
                select emp;
                break;
            case "Description":
                employees = from emp in EmployeeBL.GetAllEmployees()
                                where emp.Description.ToLower().Contains(e.Text.ToLower())
                                select emp;
                break;
            default:
                break;
        }
       ((RadComboBox)o).DataSource = employees;
  
       ((RadComboBox)o).DataBind();
   }

That way according to the column name a different query is being executed.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Gabe
Top achievements
Rank 1
answered on 14 Feb 2013, 04:09 PM
Thanks, Angel. I ended up using a case statement to do something similar earlier on in the code. My goal was to make this method fully flexible to be able to utilize it for whatever value was in the ColumnName.  This works for the stated cases so I'll mark this question answered. But if you have any thoughts on how to make this more universal, they'd be much-appreciated.
Tags
Grid
Asked by
Gabe
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Gabe
Top achievements
Rank 1
Share this question
or