filtering on a command column

23 posts, 2 answers
  1. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 14 Oct 2010 Link to this post

    can this be done?

    I  enabled filtering on my command column buy setting

    .AllowFiltering =

     

    true

    I also have a cellformatting event that will change the button text based on a date but I no longer am able to ref the cell.commandbutton as the cell type has changed from "GridCommandCellElement" to
     "GridFilterCellElement"

     

  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Oct 2010 Link to this post

    Hello Andrew,

    I don't understand exactly what you mean by this, but the CellFormatting event fires, for all the cells, and if to do some specific action if the cell element is GridCommandCellElement you can do something like:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var commandCell = e.CellElement as GridCommandCellElement;
        if (commandCell != null)
        {
            // do something here
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 14 Oct 2010 Link to this post

    yes this is what I do:

    GridCommandCellElement cellSent = (GridCommandCellElement)e.CellElement;
    package = (Package)cellSent.RowInfo.DataBoundItem;

    cellSent.CommandButton.Text =

     

    "Mark As Sent";

     

     
    then when I Allowfiltering on this column the CellElement object type has changed to "GridFilterCellElement"
    so now I need to cast the element as type "GridFilterCellElement" not "GridCommandCellElement" as above.

    GridFilterCellElement cell = (GridFilterCellElement)e.CellElement;

    by having to do this I no longer have access to the commandButton I hope this makes more sense
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Oct 2010 Link to this post

    Hello,

    You shouldn't specify .AllowFiltering on the cell Element, you can specify to allow filtering on the column itself, something like this:
    radGridView1.EnableFiltering = true;
    radGridView1.Columns[0].AllowFiltering = true;
    radGridView1.Columns[1].AllowFiltering = false;

    and if possible you should set AllowFiltering to individual columns on DataBindingComplete event, to avoid any Object reference not set to an instance of an object exceptions.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  6. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 14 Oct 2010 Link to this post

    yes that is where I do set it, when I create the column
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Oct 2010 Link to this post

    Sorry, i misunderstood in the beginning, you cannot filter by a CommandColumn because there is nothing to filter by in a CommandColumn, but i think it might work by using a CustomColumn, i will make some tests and get back to you.

    Best Regards,
    Emanuel Varga
  8. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 14 Oct 2010 Link to this post

    great thanks, in the future you should allow filtering on a command column
  9. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Oct 2010 Link to this post

    Hello again Andrew,

    OK, let's assume filtering would be enabled, it should filter based on what? the Text of the button? Value?

    Please tell me what you want to accomplish and i will try to help you achieve it.

    Best Regards,
    Emanuel Varga
  10. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 14 Oct 2010 Link to this post

    ok, well I have a button in my grid with "Mark As Sent" as the diplay text this will mark a db record as sent.
    if the record has been sent then the button is disabled but the display text would be "Sent On <datehere>"

    I change the text of the button in the "CellFormatting" event buy checking a date in my dataobject from "

    mycell.RowInfo.DataBoundItem"


    the idea was to give the user a custom filter to show only records that have been sent, have not been sent, or no filter at all buy check the date in my dataobject of the call.value property.

    I would think the cell value (bound datafield) or lets us pick if it is apply to the bound data, or the button text 
  11. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 14 Oct 2010 Link to this post

    Ok, so, to get you started, you have to create a new Column type, like so:
    public class CustomCommandColumn : GridViewCommandColumn
    {
        public CustomCommandColumn()
            : base()
        {
        }
     
        public CustomCommandColumn(string name)
            : base(name)
        {
        }
     
        public override bool AllowFiltering
        {
            get
            {
                return true;
            }
        }
    }

    We just need this, because by default the AllowFiltering of the CommandColumn is set to false.

    After that you should handle the editor Required Event, and the CellBeginEdit event:
    void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        if (radGridView1.Columns[e.ColumnIndex].Name != "Command")
        {
            return;
        }
     
        var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
        var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
        dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
    }
     
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        var editManager = sender as GridViewEditManager;
        if (editManager == null || radGridView1.CurrentColumn.Name != "Command")
        {
            return;
        }
     
        e.Editor = new RadDropDownListEditor();
        e.EditorType = typeof(RadDropDownListEditor);
    }

    This should get you started, please let me know how it is going, after this it should be just a matter of implementing a custom filtering event and showing the results.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  12. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 15 Oct 2010 Link to this post

    I have implemented what you sent, but my problem still exists: in the cellformatting event

    this is what I currently have (below)
    when I set ".EnableFiltering = true;" my cell type changes from "GridCommandCellElement" to "GridFilterCellElement"
    when that happens I lose the ".CommadButton"

    Thanks for your help on this


    GridCommandCellElement cellSent = (GridCommandCellElement)e.CellElement;
    package = (Package)cellSent.RowInfo.DataBoundItem;
                                  
    //GridFilterCellElement cell = (GridFilterCellElement)e.CellElement;
    //GridCommandCellElement cell = (GridCommandCellElement)cell2.ColumnInfo;
      
    if (package.DateSent != DateTime.MinValue)
    {
            cellSent.CommandButton.Text = "Sent On: " + package.DateSent.ToString(CONST_DATE_FORMAT);
            cellSent.CommandButton.ToolTipText = "Package was Marked As Sent On " + package.DateSent.ToString(CONST_DATE_FORMAT) + " by " + Thread.CurrentPrincipal.Identity.Name;
            cellSent.CommandButton.Enabled = false;
    }
    else
    {
            cellSent.CommandButton.Text = "Mark As Sent";
            cellSent.CommandButton.ToolTipText = "Mark Package As Sent To UWO";
            cellSent.CommandButton.Enabled = true;
    }




  13. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Oct 2010 Link to this post

    Hello again,

    It cannot change the type to that, but because you also enabled filtering on that column, the first cell that is formatted is the GridFilterCellElement, please add a check in the beginning of the CellFormatting event something like:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        var cellSent = e.CellElement as GridCommandCellElement;
        if (cellSent != null)
        {
            var package = (Package)cellSent.RowInfo.DataBoundItem;
     
            if (package.DateSent != DateTime.MinValue)
            {
                cellSent.CommandButton.Text = "Sent On: " + package.DateSent.ToString(CONST_DATE_FORMAT);
                cellSent.CommandButton.ToolTipText = "Package was Marked As Sent On " + package.DateSent.ToString(CONST_DATE_FORMAT) + " by " + Thread.CurrentPrincipal.Identity.Name;
                cellSent.CommandButton.Enabled = false;
            }
            else
            {
                cellSent.CommandButton.Text = "Mark As Sent";
                cellSent.CommandButton.ToolTipText = "Mark Package As Sent To UWO";
                cellSent.CommandButton.Enabled = true;
            }
        }
    }

    Please let me know if it's working like this for you.

    One more thing, before I forget, are you using the latest version of telerik controls? Q2 2010 SP2, ...914?

    Best Regards,
    Emanuel Varga
  14. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 15 Oct 2010 Link to this post

    ok, this is working, now I am going to try the custom filters

    yes we are using that version: 2010.2.10.914
  15. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 15 Oct 2010 Link to this post

    ok 2 things:

        1. I have now lost the abilty to sort my columns
        2. Can I add my custom filters so they show when you click on the filter Image
                - and limit the options to: no filter, sent, not sent
  16. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Oct 2010 Link to this post

    Hello again,

    First i want to apologize for the late answer,

    1. Sorting on all the columns, or just on the command column?, in my tests, sort is working normally.
    2. This here is the problem, because, the context menu that is opening is called by the grid directly, and i've been struggling to get to the sender column, in order to override this behavior for just the custom command column, but so far i have found just this brute way of doing things:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            Controls.Add(radGridView1 = new RadGridView());
     
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
            radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            radGridView1.ReadOnly = true;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
     
            radGridView1.DataSource = new TestsCollection(10);
            radGridView1.EnableFiltering = true;
        }
     
        void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            RadDropDownMenu dtMenu = new RadDropDownMenu();
     
            RadMenuItem r = default(RadMenuItem);
            RadMenuSeparatorItem sepItm = default(RadMenuSeparatorItem);
     
            //No Filter
            r = new RadMenuItem();
             
            r.Text = "No Filter";
            dtMenu.Items.Add(r);
            //r.Click += NoFilter_Click;
            //Separator
            sepItm = new RadMenuSeparatorItem();
            dtMenu.Items.Add(sepItm);
     
            r = new RadMenuItem();
            r.Text = "OK";
            dtMenu.Items.Add(r);
            //r.Click += Status0_Click;
     
            r = new RadMenuItem();
            r.Text = "Warning";
            dtMenu.Items.Add(r);
            //r.Click += Status1_Click
            e.ContextMenu = dtMenu;
     
            radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
        }
     
        void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            if (radGridView1.Columns[e.ColumnIndex].Name != "Command")
            {
                return;
            }
     
            var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
            var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
            dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
        }
     
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            var editManager = sender as GridViewEditManager;
            if (editManager == null || radGridView1.CurrentColumn.Name != "Command")
            {
                return;
            }
     
            e.Editor = new RadDropDownListEditor();
            e.EditorType = typeof(RadDropDownListEditor);
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            var column = new CustomCommandColumn("Command");
            column.CustomDataOperation = CustomDataOperation.Filtering;
            this.radGridView1.Columns.Add(column);
        }
     
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "Command" && e.CellElement as GridFilterCellElement != null)
            {
                var gridFilterElement = e.CellElement as GridFilterCellElement;
                var filterButtonElement = gridFilterElement.Children[0] as GridFilterButtonElement;
                filterButtonElement.MouseDown -= new MouseEventHandler(filterButtonElement_MouseDown);
                filterButtonElement.MouseDown += new MouseEventHandler(filterButtonElement_MouseDown);
            }
     
            var commandCell = e.CellElement as GridCommandCellElement;
            if (commandCell != null)
            {
                var test = e.CellElement.RowInfo.DataBoundItem as Test;
                if (test.Id % 2 == 0)
                {
                    commandCell.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
                    commandCell.CommandButton.TextElement.AutoSize = true;
                    commandCell.CommandButton.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                }
                else
                {
                    commandCell.Value = "Not Sent";
                    commandCell.Text = "Not Sent";
                    commandCell.CommandButton.Text = "Not Sent";
                }
            }
        }
     
        void filterButtonElement_MouseDown(object sender, MouseEventArgs e)
        {
            radGridView1.ContextMenuOpening += new ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
        }
    }
     
    #region Custom Command Column
     
    public class CustomCommandColumn : GridViewCommandColumn
    {
        public CustomCommandColumn()
            : base()
        {
        }
     
        public CustomCommandColumn(string name)
            : base(name)
        {
        }
     
        public override bool AllowFiltering
        {
            get
            {
                return true;
            }
        }
    }
     
    #endregion Custom Command Column
     
    #region Business Objects
     
    public enum MyEnum
    {
        Value1 = 0,
        Value2,
        Value3
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public MyEnum Value
        {
            get;
            set;
        }
     
        public Test(int id, string name, MyEnum value)
        {
            this.Id = id;
            this.Name = name;
            this.Value = value;
        }
    }
     
    public class TestsCollection : List<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "test" + i, (MyEnum)(i % 3)));
            }
        }
    }
     
    #endregion Business Objects

    Please let me know if this helps. Another way that would not involve always registering and unregistering to the ContextMenuOpening event would be to use a boolean flag, that's being set on filterButtonElement.MouseDown and being reset after setting the custom ContextMenu.

    Best Regards,
    Emanuel Varga
  17. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Oct 2010 Link to this post

    Hello again,

    I've updated the previous example to show the selected filter and to apply filter descriptors based on the selection,
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.Data;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            Controls.Add(radGridView1 = new RadGridView());
     
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
            radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            radGridView1.ReadOnly = true;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
     
            radGridView1.DataSource = new TestsCollection(10);
            radGridView1.EnableFiltering = true;
        }
     
        void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            RadDropDownMenu dtMenu = new RadDropDownMenu();
            var itemSelected = false;
     
            //No Filter
            var noFilterMenuItem = new RadMenuItem();
            noFilterMenuItem.Text = "No Filter";
            noFilterMenuItem.Click += new EventHandler(NoFilter_MenuItem_Click);
            dtMenu.Items.Add(noFilterMenuItem);
     
            //Separator
            var separator = new RadMenuSeparatorItem();
            dtMenu.Items.Add(separator);
     
            var menuItem = new RadMenuItem();
            menuItem.Click += new EventHandler(SentFilter_MenuItem_Click);
            menuItem.Text = "Sent";
            menuItem.IsChecked = IsFilterActive("Command", "Sent");
            dtMenu.Items.Add(menuItem);
            itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
     
            menuItem = new RadMenuItem();
            menuItem.Text = "Not Sent";
            menuItem.IsChecked = IsFilterActive("Command", "Not Sent");
            menuItem.Click += new EventHandler(NotSentFilter_MenuItem_Click);
            itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
            dtMenu.Items.Add(menuItem);
     
            noFilterMenuItem.IsChecked = !itemSelected;
     
            e.ContextMenu = dtMenu;
            radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
        }
     
        private bool IsFilterActive(string propertyName, string filterString)
        {
            foreach (var filter in radGridView1.FilterDescriptors)
            {
                if (filter.PropertyName == propertyName && filter.Value == filterString)
                {
                    return true;
                }
            }
     
            return false;
        }
     
        void NotSentFilter_MenuItem_Click(object sender, EventArgs e)
        {
            var filterFound = false;
            for (int i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
            {
                var filter = radGridView1.FilterDescriptors[i];
                if (filter.PropertyName == "Command")
                {
                    filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
                    filter.Value = "Not Sent";
                    filterFound = true;
                    break;
                }
            }
     
            if (!filterFound)
            {
                radGridView1.FilterDescriptors.Add(new FilterDescriptor("Command", FilterOperator.StartsWith, "Not Sent"));
            }
        }
     
        void SentFilter_MenuItem_Click(object sender, EventArgs e)
        {
            var filterFound = false;
            for (int i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
            {
                var filter = radGridView1.FilterDescriptors[i];
                if (filter.PropertyName == "Command")
                {
                    filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
                    filter.Value = "Sent";
                    filterFound = true;
                    break;
                }
            }
     
            if (!filterFound)
            {
                radGridView1.FilterDescriptors.Add(new FilterDescriptor("Command", FilterOperator.StartsWith, "Sent"));
            }
        }
     
        void NoFilter_MenuItem_Click(object sender, EventArgs e)
        {
            for (int i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
            {
                var filter = radGridView1.FilterDescriptors[i];
                if (filter.PropertyName == "Command")
                {
                    radGridView1.FilterDescriptors.RemoveAt(i);
                    break;
                }
            }
        }
     
        void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            if (radGridView1.Columns[e.ColumnIndex].Name != "Command")
            {
                return;
            }
     
            // because you are using a custom context menu you don't have to show the dropdown anymore
            e.Cancel = true;
            //var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
            //var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
     
            //dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
        }
     
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            var editManager = sender as GridViewEditManager;
            if (editManager == null || radGridView1.CurrentColumn.Name != "Command")
            {
                return;
            }
     
            e.Editor = new RadDropDownListEditor();
            e.EditorType = typeof(RadDropDownListEditor);
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            var column = new CustomCommandColumn("Command");
            column.FieldName = "Command";
            column.CustomDataOperation = CustomDataOperation.Filtering;
            this.radGridView1.Columns.Add(column);
        }
     
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "Command" && e.CellElement as GridFilterCellElement != null)
            {
                var gridFilterElement = e.CellElement as GridFilterCellElement;
     
                var filterButtonElement = gridFilterElement.Children[0] as GridFilterButtonElement;
                filterButtonElement.MouseDown -= new MouseEventHandler(filterButtonElement_MouseDown);
                filterButtonElement.MouseDown += new MouseEventHandler(filterButtonElement_MouseDown);
            }
     
            var commandCell = e.CellElement as GridCommandCellElement;
            if (commandCell != null)
            {
                var test = e.CellElement.RowInfo.DataBoundItem as Test;
                if (test.Id % 2 == 0)
                {
                    commandCell.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.Value = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
                    commandCell.CommandButton.TextElement.AutoSize = true;
                    commandCell.CommandButton.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                }
                else
                {
                    commandCell.Value = "Not Sent";
                    commandCell.Text = "Not Sent";
                    commandCell.CommandButton.Text = "Not Sent";
                }
            }
        }
     
        void filterButtonElement_MouseDown(object sender, MouseEventArgs e)
        {
            radGridView1.ContextMenuOpening += new ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
        }
    }
     
    #region Custom Command Column
     
    public class CustomCommandColumn : GridViewCommandColumn
    {
        public CustomCommandColumn()
            : base()
        {
        }
     
        public CustomCommandColumn(string name)
            : base(name)
        {
        }
     
        public override bool AllowFiltering
        {
            get
            {
                return true;
            }
        }
    }
     
    #endregion Custom Command Column
     
    #region Business Objects
     
    public enum MyEnum
    {
        Value1 = 0,
        Value2,
        Value3
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public MyEnum Value
        {
            get;
            set;
        }
     
        public Test(int id, string name, MyEnum value)
        {
            this.Id = id;
            this.Name = name;
            this.Value = value;
        }
    }
     
    public class TestsCollection : List<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "test" + i, (MyEnum)(i % 3)));
            }
        }
    }
     
    #endregion Business Objects

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  18. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 18 Oct 2010 Link to this post

    ok, this has worked great,

    just one other thing how can I get the current filter text to display in the column so the user can see what the current filter is with out clicking on the image
  19. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    Hello again,

    This is the updated code, I've added that filter text also in the GridFilterCellElement.
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.Data;
    using Telerik.WinControls.UI;
    using Telerik.WinControls.UI.Localization;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
     
        public Form1()
        {
            InitializeComponent();
            Controls.Add(radGridView1 = new RadGridView());
     
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            radGridView1.EditorRequired += new EditorRequiredEventHandler(radGridView1_EditorRequired);
            radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            radGridView1.ReadOnly = true;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.Dock = DockStyle.Fill;
     
            radGridView1.DataSource = new TestsCollection(10);
            radGridView1.EnableFiltering = true;
        }
     
        void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            RadDropDownMenu dtMenu = new RadDropDownMenu();
            var itemSelected = false;
     
            //No Filter
            var noFilterMenuItem = new RadMenuItem();
            noFilterMenuItem.Text = "No Filter";
            noFilterMenuItem.Click += new EventHandler(NoFilter_MenuItem_Click);
            dtMenu.Items.Add(noFilterMenuItem);
     
            //Separator
            var separator = new RadMenuSeparatorItem();
            dtMenu.Items.Add(separator);
     
            var menuItem = new RadMenuItem();
            menuItem.Click += new EventHandler(SentFilter_MenuItem_Click);
            menuItem.Text = "Sent";
            menuItem.IsChecked = IsFilterActive("Command", "Sent");
            dtMenu.Items.Add(menuItem);
            itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
     
            menuItem = new RadMenuItem();
            menuItem.Text = "Not Sent";
            menuItem.IsChecked = IsFilterActive("Command", "Not Sent");
            menuItem.Click += new EventHandler(NotSentFilter_MenuItem_Click);
            itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
            dtMenu.Items.Add(menuItem);
     
            noFilterMenuItem.IsChecked = !itemSelected;
     
            e.ContextMenu = dtMenu;
            radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
        }
     
        private bool IsFilterActive(string propertyName, string filterString)
        {
            foreach (var filter in radGridView1.FilterDescriptors)
            {
                if (filter.PropertyName == propertyName && filter.Value == filterString)
                {
                    return true;
                }
            }
     
            return false;
        }
     
        void NotSentFilter_MenuItem_Click(object sender, EventArgs e)
        {
            var filter = GetCurrentFilterForCommandColumn();
            if (filter != null)
            {
                filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
                filter.Value = "Not Sent";
            }
            else
            {
                radGridView1.FilterDescriptors.Add(new FilterDescriptor("Command", FilterOperator.StartsWith, "Not Sent"));
            }
        }
     
        void SentFilter_MenuItem_Click(object sender, EventArgs e)
        {
            var filter = GetCurrentFilterForCommandColumn();
            if (filter != null)
            {
                filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
                filter.Value = "Sent";
            }
            else
            {
                radGridView1.FilterDescriptors.Add(new FilterDescriptor("Command", FilterOperator.StartsWith, "Sent"));
            }
        }
     
        private FilterDescriptor GetCurrentFilterForCommandColumn()
        {
            for (int i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
            {
                var filter = radGridView1.FilterDescriptors[i];
                if (filter.PropertyName == "Command")
                {
                    return filter;
                }
            }
     
            return null;
        }
     
        void NoFilter_MenuItem_Click(object sender, EventArgs e)
        {
            for (int i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
            {
                var filter = radGridView1.FilterDescriptors[i];
                if (filter.PropertyName == "Command")
                {
                    radGridView1.FilterDescriptors.RemoveAt(i);
                    break;
                }
            }
        }
     
        void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            if (radGridView1.Columns[e.ColumnIndex].Name != "Command")
            {
                return;
            }
     
            // because you are using a custom context menu you don't have to show the dropdown anymore
            e.Cancel = true;
            //var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
            //var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
     
            //dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
        }
     
        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            var editManager = sender as GridViewEditManager;
            if (editManager == null || radGridView1.CurrentColumn.Name != "Command")
            {
                return;
            }
     
            e.Editor = new RadDropDownListEditor();
            e.EditorType = typeof(RadDropDownListEditor);
        }
     
        void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
        {
            var column = new CustomCommandColumn("Command");
            column.FieldName = "Command";
            column.CustomDataOperation = CustomDataOperation.Filtering;
            this.radGridView1.Columns.Add(column);
        }
     
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "Command" && e.CellElement as GridFilterCellElement != null)
            {
                var gridFilterElement = e.CellElement as GridFilterCellElement;
                var currentFilter = GetCurrentFilterForCommandColumn();
     
                gridFilterElement.FilterOperatorText.Text = (currentFilter == null ?
                    RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNoFilter) :
                    currentFilter.Value.ToString())+
                    ":";
     
                var filterButtonElement = gridFilterElement.Children[0] as GridFilterButtonElement;
                filterButtonElement.MouseDown -= new MouseEventHandler(filterButtonElement_MouseDown);
                filterButtonElement.MouseDown += new MouseEventHandler(filterButtonElement_MouseDown);
            }
     
            var commandCell = e.CellElement as GridCommandCellElement;
            if (commandCell != null)
            {
                var test = e.CellElement.RowInfo.DataBoundItem as Test;
                if (test.Id % 2 == 0)
                {
                    commandCell.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.Value = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                    commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
                    commandCell.CommandButton.TextElement.AutoSize = true;
                    commandCell.CommandButton.Text = "Sent on " + e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
                }
                else
                {
                    commandCell.Value = "Not Sent";
                    commandCell.Text = "Not Sent";
                    commandCell.CommandButton.Text = "Not Sent";
                }
            }
        }
     
        void filterButtonElement_MouseDown(object sender, MouseEventArgs e)
        {
            radGridView1.ContextMenuOpening += new ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
        }
    }
     
    #region Custom Command Column
     
    public class CustomCommandColumn : GridViewCommandColumn
    {
        public CustomCommandColumn()
            : base()
        {
        }
     
        public CustomCommandColumn(string name)
            : base(name)
        {
        }
     
        public override bool AllowFiltering
        {
            get
            {
                return true;
            }
        }
    }
     
    #endregion Custom Command Column
     
    #region Business Objects
     
    public enum MyEnum
    {
        Value1 = 0,
        Value2,
        Value3
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public MyEnum Value
        {
            get;
            set;
        }
     
        public Test(int id, string name, MyEnum value)
        {
            this.Id = id;
            this.Name = name;
            this.Value = value;
        }
    }
     
    public class TestsCollection : List<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, "test" + i, (MyEnum)(i % 3)));
            }
        }
    }
     
    #endregion Business Objects

    And just to think you almost gave up on this....

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  20. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 18 Oct 2010 Link to this post

    that worked great thanks for all your help with this
  21. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 18 Oct 2010 Link to this post

    You're very welcome, just glad that i was able to help

    and as always if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  22. Jeen
    Jeen avatar
    13 posts
    Member since:
    Jul 2014

    Posted 25 Nov 2014 Link to this post

    Hi, everyone.
    I have a question adjacent to the topic discussed here.
    I have a radgridview with several columns, the first of which is of "CheckBox" type.
    The last column is a CommandColumn, which contains buttons with detalis to show for every row (= every company listed). Below there is a separate usual button which launches some actions to accomplish, after the completion of which I need to set the state of the button of the Commandcolumn in every row to "Enabled"/"Disabled" based on the state of the first cell of the same row according to the following rule:
        if the CheckBox cell in the row is checked, then the button in the CommandColumn of this row has to be enabled,
        if the CheckBox cell in the row is not checked, then the button in the CommandColumn of this row has to be disabled,

    I have tried to set the state of the buttons using a routine in "CellFormatting" event I have found in one of the topics on this forum:

       var commandcell = e.CellElement as GridCommandCellElement;
       if (somecondition)
             commandcell.CommandButton.Enabled = true;


    However, I don't seem to be able to apply a condition based on the corresponding CheckBox cell state to modify the state of the button in the CommandColumn, using the same event handler and the same object "CellFormattingEvents e" for both.
    Is there a way to do it, or maybe some other workaround?

    Thank you very much in advance.

    Best Regards,
    Jeen B.

    P.S.: If there is a topic already created for this issue, I am sorry. Please, be so kind to redirect me to it, since I have not found it on the forum.
  23. Dess
    Admin
    Dess avatar
    1607 posts

    Posted 28 Nov 2014 Link to this post

    Hello Jeen,

    Thank you for writing.

    In the CellFormatting event you have access to the GridViewRowInfo from which you can extract the check box value for the specific row and compose the condition. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("Active", typeof(bool));
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
     
        for (int i = 0; i < 100; i++)
        {
            dt.Rows.Add(i % 2 == 0, i, "Name" + i);
        }
     
        this.radGridView1.DataSource = dt;
        GridViewCommandColumn commandColumn = new GridViewCommandColumn("CommandColumn");
        radGridView1.MasterTemplate.Columns.Add(commandColumn);
     
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.CellFormatting += radGridView1_CellFormatting;
    }
     
    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridCommandCellElement commandcell = e.CellElement as GridCommandCellElement;
        if (commandcell != null && e.Row.Cells["Active"].Value != null)
        {
            bool cbCellValue = (bool)e.Row.Cells["Active"].Value;
            if (cbCellValue)
            {
                commandcell.CommandButton.Enabled = true;
            }
            else
            {
                commandcell.CommandButton.Enabled = false;
            }
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  24. Jeen
    Jeen avatar
    13 posts
    Member since:
    Jul 2014

    Posted 01 Dec 2014 in reply to Dess Link to this post

    Hello, Desislava.
    Thank you very much!
    Your post has been of much help to me:)

    Best Regards,
    Jeen B.
Back to Top
UI for WinForms is Visual Studio 2017 Ready