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

Problem with removing filter entries

5 Answers 103 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 12 Mar 2011, 05:25 PM
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

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 13 Mar 2011, 07:44 PM
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
0
Guy
Top achievements
Rank 1
answered on 13 Mar 2011, 08:00 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 13 Mar 2011, 08:53 PM
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
0
Guy
Top achievements
Rank 1
answered on 14 Mar 2011, 10:23 AM
Richard,

That works perfectly! Thank you for your help.

Regards,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 14 Mar 2011, 10:27 AM
You're welcome. Apologies it took a couple of goes to get there.
All the best
Richard
Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Guy
Top achievements
Rank 1
Share this question
or