How to disable column insertion in SfSpreadsheet?
I want to disable column insertion while allowing row insertion.
But I can only find the following linked documentation.
https://help.syncfusion.com/wpf/spreadsheet/interactive-features
In the documentation, only the customization of the cell's right-click menu is mentioned, but there is no reference to modifying the right-click menu for entire rows or columns.
By the way, I used the following code to display only part of the right-click menu. Is there a better way to achieve this?
private void ActiveGrid_CellContextMenuOpening(object sender, CellContextMenuOpeningEventArgs e)
{
//Remove the existing Context menu
var removeNameList = new List<string>
{
// "GraphicCellPaste",
// "PasteTitleMenuItem",
// "PasteOptionsMenuItem",
"InsertMenuItem",
"DeleteMenuItem",
"ColumnWidthMenuItem",
"HideColumnMenuItem",
"UnhideColumnMenuItem",
"InsertCommentMenuItem",
"EditCommentMenuItem",
"DeleteCommentMenuItem",
"FormatCellsMenuItem",
"DefineNameMenuItem",
"InsertHyperlinkMenuItem",
"EditHyperlinkMenuItem",
"OpenHyperlinkMenuItem",
"RemoveHyperlinkMenuItem",
};
var allItems = SpreadsheetControl.SfSpreadsheet.ActiveGrid.CellContextMenu.Items;
foreach (var item in allItems)
{
if (item is ItemsControl control)
{
control.Visibility = removeNameList.Contains(control.Name) ? Visibility.Collapsed : Visibility.Visible;
}
}
}
Hi Crosslife,
Your
requirement to disable column insertion in the SfSpreadsheet can be achieved by
customizing the ContextMenuOpening event. Refer to the below code snippet,
|
//Event customization private void OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args) { //Event subscription for ContextMenuOpening this.AssociatedObject.ActiveGrid.ContextMenuOpening += OnActiveGridContextMenuOpening; }
//Event customization private void OnActiveGridContextMenuOpening(object sender, ContextMenuEventArgs e) { //Here get the context menu opened SpreadsheetGrid var Grid = sender as SpreadsheetGrid;
if (Grid != null) { if (Grid.ContextMenu != null && Grid.ContextMenu.Items.Count > 0) { //Event subscription for Insert Menu (Grid.ContextMenu.Items[6] as MenuItem).Click += OnClick; //Event subscription for Delete Menu (Grid.ContextMenu.Items[7] as MenuItem).Click += OnClick; } } }
//Event customization private void OnClick(object sender, RoutedEventArgs e) { //Here use the Dispatcher to access the InsertDeleteCellsWindow this.AssociatedObject.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { //Her get the InsertDeleteCellsWindow var windows = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
if (windows != null) { //Here get the InsertDeleteCellsDialog var insertDeleteCellsWindow = windows.Content as InsertDeleteCellsDialog; if (insertDeleteCellsWindow != null) { //Here get the EntireColumn radio button var entireColumn = (RadioButton)insertDeleteCellsWindow.GetType().GetField("EntireColumn", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(insertDeleteCellsWindow);
//Here disable the EntireColumn radio button if (entireColumn != null) entireColumn.IsEnabled = false; } } })); }
|
Find the sample demo in the attachment.
Regards,
Vijayarasan S
If
this post is helpful, please consider Accepting it as the solution so that
other members can locate it more quickly.
Attachment: SfSpreadSheetDemo_29fe9400.zip
Hi crosslife,
We apologize for the inconvenience caused. Previously, we had disabled column insertion for specific cells. Now, we have extended this behavior to include the Column Header Row as well. We have attached the modified sample and a video reference for your review.
Please refer the modified Code Snippet.
//Event customization
private void OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
{
//Event subscription for ContextMenuOpening
this.AssociatedObject.ActiveGrid.ContextMenuOpening += OnActiveGridContextMenuOpening;
}
//Event customization
private void OnActiveGridContextMenuOpening(object sender, ContextMenuEventArgs e)
{
//Here get the context menu opened SpreadsheetGrid
var Grid = sender as SpreadsheetGrid;
if (Grid != null)
{
if (Grid.ContextMenu != null && Grid.ContextMenu.Items.Count > 0)
{
//if ContextMenu opened from HeaderRow
if (Grid.SelectedRanges.Count > 0 && Grid.SelectedRanges.ActiveRange != null && Grid.SelectedRanges.ActiveRange.IsCols)
{
//Disabling Insert MenuItem
(Grid.ContextMenu.Items[6] as MenuItem).IsEnabled = false;
//Disabling Delete MenuItem,
(Grid.ContextMenu.Items[7] as MenuItem).IsEnabled = false;
}
else
{
//Enabling Insert MenuItem
(Grid.ContextMenu.Items[6] as MenuItem).IsEnabled = true;
//Enabling Delete MenuItem,
(Grid.ContextMenu.Items[7] as MenuItem).IsEnabled = true;
//Event subscription for Insert Menu
(Grid.ContextMenu.Items[6] as MenuItem).Click += OnClick;
//Event subscription for Delete Menu
(Grid.ContextMenu.Items[7] as MenuItem).Click += OnClick;
}
}
}
}
//Event customization
private void OnClick(object sender, RoutedEventArgs e)
{
//Here use the Dispatcher to access the InsertDeleteCellsWindow
this.AssociatedObject.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
//Her get the InsertDeleteCellsWindow
var windows = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
if (windows != null)
{
//Here get the InsertDeleteCellsDialog
var insertDeleteCellsWindow = windows.Content as InsertDeleteCellsDialog;
if (insertDeleteCellsWindow != null)
{
//Here get the EntireColumn radio button
var entireColumn = (RadioButton)insertDeleteCellsWindow.GetType().GetField("EntireColumn", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(insertDeleteCellsWindow);
//Here disable the EntireColumn radio button
if (entireColumn != null)
entireColumn.IsEnabled = false;
}
}
}));
}
If we have misunderstood any aspect of your needs, kindly provide additional details about the issues you're encountering. Your feedback will help us better understand the problem and provide an appropriate solution.
We look forward to your response.
Regards,
Manikanda Akash M
Attachment: SfSpreadSheet_Demo_8d4febeb.zip
- 3 Replies
- 3 Participants
-
CR crosslife
- Sep 14, 2024 03:34 AM UTC
- Sep 18, 2024 01:06 PM UTC