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

Inital filter for dynamically added custom GridViewDecimalColumn

5 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nicole
Top achievements
Rank 1
Nicole asked on 09 Sep 2011, 11:01 AM
I have implemented my custom column, inheriting from GridViewDecimalColumn. The code looks like this:

public class DynamicDecimalColumn : GridViewDecimalColumn, IDynamicColumn
    {
        public DynamicDecimalColumn(string fieldName, string attributeName)
            : base(fieldName)
        {
            AttributeName = attributeName;
            DataType = typeof(decimal);
        }
  
        protected override void Initialize()
        {
            base.Initialize();
        }
  
        public string AttributeName { get; set; }
  
  
        public override Type GetCellType(GridViewRowInfo row)
        {
            if (row is GridViewDataRowInfo)
            {
                return typeof(DynamicPropertyCellElement);
            }
            return base.GetCellType(row);
        }
    }

 I am adding a DynamicDecimalColumn by using code like
DynamicDecimalColumn column = new DynamicDecimalColumn("OwnerCode" );
column.DecimalPlaces = 0
column.Name = "OwnerCode";
column.HeaderText = "OwnerCode";
_gridViewNetSegments.MasterTemplate.Columns.Add(column);

The column is defined as filterable. When I start filtering by typing a filter text, the system is using a "contains" filter (LIKE) by default, and so I get an error. When I open the context menu to change the filter, the "Contains" filter ist not available, as expected. How can I change the initial filter by code, as I would like the "Equals" filter as the initial filter ? 

Nicole 

5 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Sep 2011, 07:40 AM
Hello Nicole,

I've helped in the past to create a filterable command column, please take a look at this thread, it should provide you with everything you need.

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 14 Sep 2011, 04:30 PM
Hello Nicole,

Thank you for your question.

Emanuel's solution could help you customize the filtering of your custom column, but in general the default filter operator for a column, derived from GridViewDecimalColumn, should be 'Equals'. I have attached a sample project which uses your custom column with one modification - using GridDataCellElement instead of DynamicPropertyCellElement (as I do not have its implementation).

The default column filter in the attached sample is 'Equals'. Please could you give us more details regarding your solution using this custom column? It will help us investigate this case and find solution for it. Thank you in advance.

I am looking forward to your reply.

Best regards,
Alexander
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Nicole
Top achievements
Rank 1
answered on 15 Sep 2011, 11:05 AM
Hi Alexander

I have an idea where the problem could come from, and its dependent on our own implementation. My column is bound to an attribute "DynamicAttributes" containing a dictionary with key/value pairs. 
In our overwritten GridViewDecimalColumn we store the key of the dictionary value we would like to display in the grid.

And we have overwritten the property "Value" of the GridDataCellElement like that.

public override object Value
        {
            get
            {
                IDynamicColumn column = base.DataColumnInfo as IDynamicColumn;
                if (column != null)
                {
                    // we read the dictionary key name from the column definition
                    string attributeName = column.AttributeName;
                    Dictionary<string,object> entries = (Dictionary<string,object>)base.Value;
                    return entries[attributeName];
                }
                return 0;
            }
            set
            {
                base.Value = value;
            }
        }

Our grid has now two columns, both bound to the property "DynamicAttributes". The first column is  is inheriting from GridViewDecimalColumn (because the vaule in the dictionary is a number) and the second column is inheriting from GridViewTextBoxColumn (because the value in the dictionary is a string). What I have already seen is that if you apply a filter to one row, the filter text appears in the filter box of the other column. I think because they are both bound to the same property, the system implies that theyshouild be treated the same.
And maybe it happens the same with the default filter. As the second column ist the textbox column, the default filter ist "LIKE", and so the LIKE-filter will be set for both columns.
  
We overcome the problem by overwriting the property FilterDescriptor for each column, and initialise the FilterOperator on creation of the column. The FilterDescriptor is now a private property of the column, and it has nothing to do with the FilterDescriptor of the base class.

Further, we we had to overwrite PerformFiltering() of the grid as described in your help. It looks more or less stable at the moment, but not sure if everything will work as expected. It would be nice if the filters would not get applied to all colums with the same binding attribute, but I guess other solutions are dependent on that.

bye, Nicole
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Sep 2011, 12:17 PM
Hello again,

In the thread I've posted, if I remember correctly, filtering was handled by code... i might be wrong, so if you need something different just start from there and create the filters that fit you're requirements.

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 20 Sep 2011, 10:07 AM
Hello Nicole,

Thank you for the provided details.

As you have correctly considered, the filter descriptors of RadGridView perform their operations according to the FieldName of the columns. I am not familiar with your requirements, but in general, you can bind RadGridView to a dictionary and its columns to the key and the value of the dictionary:
Dictionary<decimal, string> dict = new Dictionary<decimal, string>();
dict.Add(1, "A");
dict.Add(2, "B");
dict.Add(3, "C");
 
this.radGridView1.EnableFiltering = true;
this.radGridView1.AutoGenerateColumns = false;
 
this.radGridView1.Columns.Add(new GridViewDecimalColumn("Key"));
this.radGridView1.Columns.Add(new GridViewTextBoxColumn("Value"));
 
this.radGridView1.DataSource = dict;

If the dictionary is a property in the object you bind RadGridView to, you can use the Binding to Sub Objects capabilities of the control.

I hope it helps you simplify your implementation.

Best regards,
Alexander
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Nicole
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Nicole
Top achievements
Rank 1
Share this question
or