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

RadDropDownListEditorElement.Filter

8 Answers 208 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David Simmonds
Top achievements
Rank 1
David Simmonds asked on 28 Feb 2012, 06:35 PM
I am using a licensed version of 2011.Q2 so it's not the latest but it's all that I have for the time being.

I set a filter callback for a ComboBoxColumn in the CellEditorInitialized event handler like this:

 

RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
if (editor != null)
{
    RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
    editorElement.Filter = FilterWrappers;
}

 

This works fine. The problem is that I have another ComboBoxColumn with no filter on it. When I click in the first column and select a value and then click in the second column the filter method for the first column is called. It is called once for every item in the combo but the entries contain items from the first combo and items from the second combo. Why is this happening?

8 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 02 Mar 2012, 03:01 PM
Hi David,

Thank you for writing.

I assume that this is happening, because the posted code which I assume is in the CellEditorInitialized event is not restricted to be executed only for the desired column. In other words, please try the following snippet, and just modify the YourFirstColumnName string to the name of the column, where you want the filter applied:
void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    if (e.Column.Name == "YourFirstColumnName")
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
            editorElement.Filter = FilterWrappers;
        }
    }
}

Let me know how this works for you.
 
Regards,
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
David Simmonds
Top achievements
Rank 1
answered on 02 Mar 2012, 05:24 PM
I am already doing that. I check for the column name first. So the issue still exists.
0
David Simmonds
Top achievements
Rank 1
answered on 06 Mar 2012, 06:38 PM
Since the suggestion given was already implemented and I still get the same results, are there any other suggestions?
0
Julian Benkov
Telerik team
answered on 07 Mar 2012, 11:06 AM
Hello David,

In order to be performance optimized and to consume less memory, RadGridView control uses one ComboBox editor in this case and the filter delegate will be available to another column too. To address the issue, you can Reset the filter delegate for other columns:

void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
    if (e.Column.Name == "YourFirstColumnName")
    {
        if (editor != null)
        {
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
            editorElement.Filter = FilterWrappers;
        }
    }
    else
    {
        if (editor != null)
        {
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
            editorElement.Filter = null;
        }
    }
}

I hope this helps. Do not hesitate to contact us if you have further questions or issues. 

Kind regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
David Simmonds
Top achievements
Rank 1
answered on 07 Mar 2012, 10:58 PM

See the code below. What happens is that if I select the WRAPPER column, then I select the PHASE column, and then select the WRAPPER column again, I see item from the PHASE column in the filter for the WRAPPER. The problem comes when it tries to convert one of the items to an int. The WRAPPER column contains int values and the PHASE column contains string value. As you can see I have cleared the Filter property. I put this code in as a hail-mary attempt to make this work but it failed. Please attempt to explain why this is not working.

private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (e.Column is GridViewComboBoxColumn)
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
            editorElement.Filter = null;
        }
    }
    switch (e.Column.Name)
    {
        case "WRAPPER":
            {
                GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                cbc.DataSource = dataWrappers;
                RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
                if (editor != null)
                {
                    RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                    editorElement.Filter = FilterWrappers;
                }
            }
            break;
        case "PHASE":
            {
                GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                cbc.DataSource = dataPhases;
            }
            break;
    }
}
private bool FilterWrappers(RadListDataItem item)
{
    // cannot select a wrapper if an HTC is not selected first
    if (CurrentItem.HEAT_TRACE_CONTROLLER_NO == null ||
        CurrentItem.HEAT_TRACE_CONTROLLER_NO.Length == 0)
    {
        return false;
    }
    int? nWrapper = null;
    if (item.Value.ToString() != String.Empty)
        nWrapper = Convert.ToInt32(item.Value);
    // if it is PAC or if settings allow multiple items to be 
    // connected to a wrapper, then let the wrapper be used
    if (EHTS.Properties.Settings.Default.CircuitAssignment_AllowMultipleLTSItemsPerWrapper ||
        CurrentItem.CONTROL_TYPE == "PAC")
    {
        return true;
    }
    // determine if the wrapper is already assigned to another power point
    var data = (from gi in GridItems
                where gi.WRAPPER == nWrapper &&
                        gi.ID != CurrentItem.ID &&
                        gi.HEAT_TRACE_CONTROLLER_NO == CurrentItem.HEAT_TRACE_CONTROLLER_NO
                select gi).FirstOrDefault();
    // if data is not null, then the wrapper is already used
    return (data == null);
}
0
Julian Benkov
Telerik team
answered on 12 Mar 2012, 04:00 PM
Hello David,

Thank you for the provided information. However, it did not allow us to reproduce the case and assist you accordingly. Therefore, I would kindly ask you to open a new support ticket and attach a sample project with some part of your data. This will allow to analyze your case in depth, hence provide you with further assistance.

Thank you for your time and cooperation.

Greetings,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
David Simmonds
Top achievements
Rank 1
answered on 12 Mar 2012, 04:59 PM
The following code exhibits the problem. All you have to do is click the Wrapper column cell and bring down the dropdown. Then select the Phase column. You will get the app to crash in the FilterWrappers method. Please explain why this is happening. This is in a sample application that I built. If I could upload the zip file for the entire project I would.

I cannot submit a ticket since my support contract has expired and we have not renewed it yet. It is in the process though.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
namespace RadControlsWinFormsApp2
{
    public partial class Form1 : Form
    {
        private String[] dataPhases = null;
        private String[] dataWrappers = null;
  
        public class Data
        {
            public Data(int ID, string Phase, int Wrapper)
            {
                this.ID = ID;
                this.Phase = Phase;
                this.Wrapper = Wrapper;
            }
            public int ID { get; set; }
            public String Phase { get; set; }
            public int Wrapper { get; set; }
        }
        public Form1()
        {
            InitializeComponent();
  
            this.radGridView1.DataSource = new Data[] { new Data(1, "A", 1), new Data(2, "B", 2) };
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            GridViewComboBoxColumn columnPhase = radGridView1.Columns["PHASE"] as GridViewComboBoxColumn;
            dataPhases = new String[] { "A", "B", "C", "A/B/C" };
  
            GridViewComboBoxColumn columnWrapper = radGridView1.Columns["WRAPPER"] as GridViewComboBoxColumn;
            dataWrappers = new String[] { "", "1", "2" };
  
            columnWrapper.DataSource = dataWrappers;
            columnPhase.DataSource = dataPhases;
        }
  
        private void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (e.Column is GridViewComboBoxColumn)
            {
                RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
                if (editor != null)
                {
                    RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                    editorElement.Filter = null;
                }
            }
            switch (e.Column.Name)
            {
                case "WRAPPER":
                    {
                        GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                        cbc.DataSource = dataWrappers;
  
                        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
                        if (editor != null)
                        {
                            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                            editorElement.Filter = FilterWrappers;
                        }
                    }
                    break;
                case "PHASE":
                    {
                        GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                        cbc.DataSource = dataPhases;
                    }
                    break;
            }
        }
  
        private bool FilterWrappers(RadListDataItem item)
        {
            int nWrapper = 0;
            if (item.Value.ToString() != String.Empty)
                nWrapper = Convert.ToInt32(item.Value);
            return true;
        }
    }
}

namespace RadControlsWinFormsApp2
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn();
            Telerik.WinControls.UI.GridViewComboBoxColumn gridViewComboBoxColumn1 = new Telerik.WinControls.UI.GridViewComboBoxColumn();
            Telerik.WinControls.UI.GridViewComboBoxColumn gridViewComboBoxColumn2 = new Telerik.WinControls.UI.GridViewComboBoxColumn();
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.radGridView1.Location = new System.Drawing.Point(13, 13);
            // 
            // radGridView1
            // 
            gridViewTextBoxColumn1.FieldName = "ID";
            gridViewTextBoxColumn1.HeaderText = "ID";
            gridViewTextBoxColumn1.Name = "columnID";
            gridViewComboBoxColumn1.DisplayMember = "Wrapper";
            gridViewComboBoxColumn1.FieldName = "Wrapper";
            gridViewComboBoxColumn1.HeaderText = "Wrapper";
            gridViewComboBoxColumn1.Name = "WRAPPER";
            gridViewComboBoxColumn1.ValueMember = "Wrapper";
            gridViewComboBoxColumn1.Width = 100;
            gridViewComboBoxColumn2.DisplayMember = "Phase";
            gridViewComboBoxColumn2.FieldName = "Phase";
            gridViewComboBoxColumn2.HeaderText = "Phase";
            gridViewComboBoxColumn2.Name = "PHASE";
            gridViewComboBoxColumn2.ValueMember = "Phase";
            gridViewComboBoxColumn2.Width = 100;
            this.radGridView1.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] {
            gridViewTextBoxColumn1,
            gridViewComboBoxColumn1,
            gridViewComboBoxColumn2});
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(588, 248);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellEditorInitialized += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEditorInitialized);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(613, 273);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

0
Accepted
Julian Benkov
Telerik team
answered on 15 Mar 2012, 11:12 AM
Hi David,

I debugged the sample project and found the synchronization issues in our DropDownEditor when the filter is applied and DataSource or SelectedItem is changed internally. I logged the issue in our Public Issue Tracking System. It will be addressed in one of the next our releases after the upcoming Q1 2012 SP1 release. Currently, you can use EditorRequired event to work around the issue:
using System;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.GridView
{
    public partial class DropDownListEditorFiltering : Form
    {
        private String[] dataPhases = null;
        private String[] dataWrappers = null;
 
        public class Data
        {
            public Data(int ID, string Phase, int Wrapper)
            {
                this.ID = ID;
                this.Phase = Phase;
                this.Wrapper = Wrapper;
            }
            public int ID { get; set; }
            public String Phase { get; set; }
            public int Wrapper { get; set; }
        }
        public DropDownListEditorFiltering()
        {
            InitializeComponent();
 
            this.radGridView1.EditorRequired += radGridView1_EditorRequired;
            this.radGridView1.DataSource = new Data[] { new Data(1, "A", 1), new Data(2, "B", 2) };
        }
 
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (radGridView1.CurrentColumn.Name == "PHASE" || radGridView1.CurrentColumn.Name == "WRAPPER")
            {
                e.Editor = new MyEditor();
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            GridViewComboBoxColumn columnPhase = radGridView1.Columns["PHASE"] as GridViewComboBoxColumn;
            dataPhases = new String[] { "A", "B", "C", "A/B/C" };
 
            GridViewComboBoxColumn columnWrapper = radGridView1.Columns["WRAPPER"] as GridViewComboBoxColumn;
            dataWrappers = new String[] { "", "1", "2" };
 
            columnWrapper.DataSource = dataWrappers;
            columnPhase.DataSource = dataPhases;
        }
 
        private void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            switch (e.Column.Name)
            {
                case "WRAPPER":
                    {
                        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
                        if (editor != null)
                        {
                            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
                            editorElement.Filter = FilterWrappers;
                        }
 
                        GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                        cbc.DataSource = dataWrappers;
                    }
                    break;
                case "PHASE":
                    {
                        GridViewComboBoxColumn cbc = e.Column as GridViewComboBoxColumn;
                        cbc.DataSource = dataPhases;
                    }
                    break;
            }
        }
 
        private bool FilterWrappers(RadListDataItem item)
        {
            int nWrapper = 0;
            if (item.Value.ToString() != String.Empty)
                nWrapper = Convert.ToInt32(item.Value);
 
            return true;
        }
 
        public class MyEditor : Telerik.WinControls.UI.RadDropDownListEditor
        {
            public override bool EndEdit()
            {
                RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)this.EditorElement;
                editorElement.Filter = null;
                
                return base.EndEdit();
            }
        }
    }
}

Thank you for the sample code. Your Telerik points have been updated.

All the best,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
David Simmonds
Top achievements
Rank 1
Answers by
Stefan
Telerik team
David Simmonds
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or