Problem with removing filter entries

6 posts, 1 answers
  1. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 12 Mar 2011 Link to this post

    Afternoon,

    I'm having a small issue getting my code to function properly and wondered if someone could let me know where I'm going wrong.

    Within my project are a couple of columns which hold dates. What I am trying to do is remove the "Is null" and "Is not null" options from the filter list for those columns.

    Having searched the forums and read through some of the documentation I have come up with the below code.

    private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
            {
      
                if (this.radGridView1.CurrentCell is GridDataCellElement || this.radGridView1.CurrentCell is GridFilterCellElement && e.ContextMenuProvider == null)
                {
                    
                    if (((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "DateCalled" ||
                        ((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "NextCallDate")
                    {
                        if (e.ContextMenu.Items.Count > 0)
                        {
                            if (e.ContextMenu.Items[0] is Telerik.WinControls.UI.RadFilterOperationMenuItem)
                            {
                                foreach (Telerik.WinControls.UI.RadMenuItemBase menuItem in e.ContextMenu.Items)
                                {
                                    if (string.Equals("Is null", menuItem.Text) || string.Equals("Is not null", menuItem.Text))
                                    {
                                        menuItem.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                                    }
                                }
                            }
                        }
                    }
                }
            }

    This works perfectly fine if I right click on the filter button for either of those columns. However, if I left click it doesn't correctly register the column FieldName. Instead it will report the FieldName of the column which has an entry selected within the GridView (Example would be that I have a company name selected with the grid view, left click the filter button for the next call date and it will think that the FieldName is CompanyName instead).

    Am I doing it completely wrong or am I just missing something?

    Regards,

    Guy
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 13 Mar 2011 Link to this post

    Hi Guy,

    You were almost there. Just need to check that the ContextMenuProvider == null will ensure that you are on a filtering context menu.
    Please try this and let me know if that's ok for you.

    private void radGridView1_ContextMenuOpening_1(object sender, ContextMenuOpeningEventArgs e)
    {
        if (e.ContextMenuProvider == null)
        {
           if (((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "ColumnName1" ||
            ((GridViewDataColumn)this.radGridView1.CurrentColumn).FieldName == "ColumnName2")
            {
                if (e.ContextMenu.Items.Count > 0)
                {
                    foreach (Telerik.WinControls.UI.RadMenuItemBase menuItem in e.ContextMenu.Items)
                    {
                        if (string.Equals("Is null", menuItem.Text) || string.Equals("Is not null", menuItem.Text))
                        {
                            menuItem.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                        }
                    }
                }
            }
        }
    }

    Hope that helps but let me know if you need more information
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 13 Mar 2011 Link to this post

    Hi Richard,

    Thanks for the reply. I have made the adjustment to my code but it does not work as intended if a row is selected but not an item that's in either of the two targeted columns.

    If I right click one of the filter buttons in either columns it is fine but if I have a cell highlighted from a different column left click displays the menu items still. If I right click one of the filter buttons and then left click it does work but that's because that column has focus and is reading the FieldName.

    Regards,

    Guy
  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 13 Mar 2011 Link to this post

    Hi Guy,

    Yes, you're right. This is because the current column is not one of those that you have in your named values.

    There may well be other ways to do this but this works fine for me. Please can you try the following

    private string m_ColumnName = "";
    private void radGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.radGridView1.ElementTree.GetElementAtPoint(new Point(e.X, e.Y)) is GridFilterButtonElement)
        {
            GridFilterButtonElement element = (GridFilterButtonElement)this.radGridView1.ElementTree.GetElementAtPoint(new Point(e.X, e.Y));
            m_ColumnName = ((GridFilterCellElement)element.Parent).ColumnInfo.Name;
        }
    }

    private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        if (e.ContextMenuProvider == null)
        {
            if (m_ColumnName == "ColumnName1" || m_ColumnName == "ColumnName2")
            {
                if (e.ContextMenu.Items.Count > 0)
                {
                    foreach (Telerik.WinControls.UI.RadMenuItemBase menuItem in e.ContextMenu.Items)
                    {
                        if (string.Equals("Is null", menuItem.Text) || string.Equals("Is not null", menuItem.Text))
                        {
                            menuItem.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                        }
                    }
                }
            }
        }
    }

    Hope that helps
    Richard
  6. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 14 Mar 2011 Link to this post

    Richard,

    That works perfectly! Thank you for your help.

    Regards,

    Guy
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Mar 2011 Link to this post

    You're welcome. Apologies it took a couple of goes to get there.
    All the best
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready