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

AllowClearCell

7 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mathieu Yargeau
Top achievements
Rank 1
Mathieu Yargeau asked on 17 Mar 2010, 03:00 PM
Hello,

I was wondering if there was a AllowClearCell property (or something similar). I don't want the user to right-click on a cell and select the Clear Value function on some on my cells. Is there a way other than the event CellValueChanged?

If the person wants to clear the value of a cell, I want him to click in it and edit the value to leave nothing in it. I want this mainly because I have code in the CellEndEdit and because I don't want the values in my ComboBoxColumn to be cleared (It must be one of the values in the list).

7 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 17 Mar 2010, 06:26 PM
Hi Mathieu Yargeau,

Yes, there is a way. You should handle ContextMenuOpening event and disable or hide the "Clear value" menu item. Here is a sample:

void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    foreach (RadMenuItemBase item in e.ContextMenu.Items)
    {
        if (item.Text == "Clear Value")
        {
            item.Enabled = false;
            break;
        }
    }
}

 

Best wishes,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mathieu Yargeau
Top achievements
Rank 1
answered on 17 Mar 2010, 06:50 PM
Great, I didn't see that event. Thanks.

It works, but I need to find another way to identify the menu item. I can't use the Text property in my case since I have a class that inherits RadGridLocalizationProvider and overrides the GetLocalizedString. That override returns a string depending on the current active language of the application, so the item text can be "Clear Value" in any of the 35 languages...

Normally, I would use something like the Tag or the Name property, but the Name property of the item is always an empty string for some reason, and the Tag is always null.

Is there a way to identify the menu item without the text? Is there a way to access the menu item's id to compare it to RadGridStringId.ClearValueMenuItem?
0
Accepted
Jack
Telerik team
answered on 19 Mar 2010, 05:57 PM
Hi Mathieu Yargeau,

In this case you can use the RadGridLocalizationProvider class to get the localized string. Here is the modified code:
void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    string clearValueString = Telerik.WinControls.UI.Localization.RadGridLocalizationProvider.CurrentProvider.
        GetLocalizedString(Telerik.WinControls.UI.Localization.RadGridStringId.ClearValueMenuItem);               
 
    foreach (RadMenuItemBase item in e.ContextMenu.Items)
    {
        if (item.Text == clearValueString)
        {
            item.Enabled = false;
            break;
        }
    }
}

 

Greetings,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mathieu Yargeau
Top achievements
Rank 1
answered on 19 Mar 2010, 06:20 PM
Oh, that's great. Thanks.
0
xclirion
Top achievements
Rank 1
answered on 25 Oct 2010, 10:36 AM
Hi,

is it also possible to remove the item from the context menu? I've tried to make it invisible. This works but it doesn't reduce the size of the context menu. And if you click on a row and - as in my case - the item is the delete Row item, then you've got an empty one line context menu.

Disabling the item would be a solution, but customers may ask under what conditions this will be enabled.

Thanks,
Michael
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 25 Oct 2010, 02:14 PM
Hello Michael Topf ,

You can either remove a specific item from the context menu on the ContextMenuOpening event like this:
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        for (int i = e.ContextMenu.Items.Count - 1; i >= 0; i--)
        {
            var item = e.ContextMenu.Items[i];
            if (item.Text.Contains("Sort"))
            {
                e.ContextMenu.Items.Remove(item);
                // or set it's visibility to Collapsed
                //item.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
            }
        }
 
        if (e.ContextMenu.Items[0] is RadMenuSeparatorItem)
        {
            e.ContextMenu.Items.RemoveAt(0);
            //or
            //e.ContextMenu.Items[0].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
    }
}

or you can just set it's visibility state to Collapsed, not Hidden, like so:
e.ContextMenu.Items[0].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;

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

Best Regards,
Emanuel Varga
0
xclirion
Top achievements
Rank 1
answered on 26 Oct 2010, 05:59 AM
Hi Emanuel Varga,

thanks for your solution, it works perfectly.

I've played around a little bit with this and tried to add a new menu item with this code:

// Add a new menu item
var validateCellItem = new RadItem();
validateCellItem.Text = "Validate Cell";
validateCellItem.Visibility = ElementVisibility.Visible;
validateCellItem.Click += new EventHandler(validateCellItem_Click);
e.ContextMenu.Items.Add(validateCellItem);

But this doesn't seem to work. There's no error, but also no new menu item. Any idea how to solve this?

Thanks,
Michael
Tags
GridView
Asked by
Mathieu Yargeau
Top achievements
Rank 1
Answers by
Jack
Telerik team
Mathieu Yargeau
Top achievements
Rank 1
xclirion
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or