-
Data Grid
- Overview
-
Data Binding
-
Paging and Scrolling
-
Editing
-
Grouping
-
Filtering and Sorting
- Focused Row
-
Row Drag & Drop
-
Selection
-
Columns
- State Persistence
-
Appearance
-
Templates
-
Data Summaries
-
Master-Detail
-
Export to PDF
-
Export to Excel
-
Adaptability
- Keyboard Navigation
-
Pivot Grid
- Overview
-
Data Binding
-
Field Chooser
-
Features
-
Export to Excel
-
Tree List
- Overview
-
Data Binding
- Sorting
- Paging
-
Editing
- Node Drag & Drop
- Focused Row
-
Selection
-
Filtering
-
Column Customization
- State Persistence
- Adaptability
- Keyboard Navigation
-
Scheduler
- Overview
-
Data Binding
-
Views
-
Features
- Virtual Scrolling
-
Grouping
-
Customization
- Adaptability
-
Html Editor
-
Chat
-
Diagram
- Overview
-
Data Binding
-
Featured Shapes
-
Custom Shapes
-
Document Capabilities
-
User Interaction
- UI Customization
- Adaptability
-
Charts
- Overview
-
Data Binding
-
Area Charts
-
Bar Charts
- Bullet Charts
-
Doughnut Charts
-
Financial Charts
-
Line Charts
-
Pie Charts
-
Point Charts
-
Polar and Radar Charts
-
Range Charts
-
Sparkline Charts
-
Tree Map
-
Funnel and Pyramid Charts
- Sankey Chart
-
Combinations
-
More Features
-
Export
-
Selection
-
Tooltips
-
Zooming
-
-
Gantt
- Overview
-
Data
-
UI Customization
- Strip Lines
- Export to PDF
- Sorting
-
Filtering
-
Reporting
- AI-powered Extensions
-
Interaction
-
Report Types
-
Data binding
-
Real-life Reports
-
Layout Features
-
Report Controls
-
Web-specific Features
-
Rich Text Editor
- Overview
- Load/Save
- Document Protection
-
Templates
- Autocorrect
-
Customization
- Simple View
-
Spreadsheet
- Overview
-
Open a Document
- Export And Printing
-
Features
-
UI Customization
-
Gauges
- Overview
-
Data Binding
-
Bar Gauge
-
Circular Gauge
-
Linear Gauge
-
Navigation
- Overview
- Accordion
-
Menu
- Multi View
-
Drawer
-
Tab Panel
-
Tabs
-
Toolbar
- Pagination
-
Tree View
- Right-to-Left Support
-
Layout
-
Tile View
- Splitter
-
Gallery
- Scroll View
- Resizable
-
-
Editors
- Overview
- Autocomplete
-
Calendar
- Check Box
- Color Box
- Date Box
-
Date Range Box
-
Drop Down Box
-
Number Box
-
Select Box
- Switch
-
Tag Box
- Text Area
- Text Box
- Validation
- Custom Text Editor Buttons
- Right-to-Left Support
- Editor Appearance Variants
-
Forms and Multi-Purpose
- Overview
- Button Group
- Field Set
-
Filter Builder
-
Form
- Radio Group
-
Range Selector
- Numeric Scale (Lightweight)
- Numeric Scale
- Date-Time Scale (Lightweight)
- Date-Time Scale
- Logarithmic Scale
- Discrete scale
- Custom Formatting
- Use Range Selection for Calculation
- Use Range Selection for Filtering
- Image on Background
- Chart on Background
- Customized Chart on Background
- Chart on Background with Series Template
- Range Slider
- Slider
-
Sortable
-
File Management
-
File Manager
- Overview
-
File System Types
-
Customization
-
File Uploader
-
-
Actions and Lists
-
Maps
- Overview
-
Map
-
Vector Map
-
Dialogs and Notifications
-
Localization
Related Demos:
Your search did not match any results.
Spreadsheet - Context Menu
This demo shows how to implement a custom context menu and display it for specified sheet cells only.
To populate the context menu with custom items, write the OnPopupMenuShowing event handler. You can add different items to the menu based on the cell data type (such as weight, currency, or discount values). Use the OnCustomCommandExecuted event to process an item click. The commandName event argument property allows you to identify the clicked item.
Was this demo helpful?
Feel free to share demo-related thoughts here.
If you have technical questions, please create a support ticket in the DevExpress Support Center.
Thank you for the feedback!
If you have technical questions, please create a support ticket in the DevExpress Support Center.
Backend API
@inject IWebHostEnvironment env
<script src="~/DemosScripts/ContextMenuCustomization.js" asp-append-version="true"></script>
<div id="popoverContainer" class="custom-popover">
</div>
@{
var documentPath = System.IO.Path.Combine(
env.ContentRootPath,
DirectoryManagmentUtils.GetDocumentSampleFolderPath(Context), "ContextMenu.xlsx");
}
@(Html.DevExpress()
.Spreadsheet("spreadsheet")
.Height("500px")
.Width("100%")
.ConfirmOnLosingChanges(confirm => confirm.Enabled(false))
.DocumentRequestHandlerUrl(Url.Action("DxDocumentRequest"))
.Open(documentPath)
.ClientSideEvents(events => {
events.OnPopupMenuShowing("SpreadsheetPopupMenuShowing");
events.OnCustomCommandExecuted("SpreadsheetCustomCommandExecuted");
})
)
<script>
$("#popoverContainer").dxPopover({
position: "right",
maxWidth: 230
});
function getContextMenuCustomData(onSuccess) {
var spreadsheetState = spreadsheet.getSpreadsheetState();
$.ajax({
type: 'POST',
url: '@Url.Action("GetContextMenuCustomData")',
data: { spreadsheetState: spreadsheetState, rowIndex: spreadsheet.getSelection().activeCellRowIndex },
success:function(data) {
onSuccess(data);
}
});
}
</script>
using System;
using System.IO;
using System.Linq;
using System.Text;
using DevExpress.AspNetCore.Spreadsheet;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet.Export;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace AspNetCoreDemos.Spreadsheet {
[Route("[action]")]
public class SpreadsheetController : Controller {
public ActionResult ContextMenuCustomization() {
return View();
}
[HttpPost]
public IActionResult GetContextMenuCustomData(SpreadsheetClientState spreadsheetState, int rowIndex) {
var spreadsheet = SpreadsheetRequestProcessor.GetSpreadsheetFromState(spreadsheetState);
Worksheet activeWorksheet = spreadsheet.Document.Worksheets.ActiveWorksheet;
rowIndex = rowIndex + 1;
var discountInfoRange = activeWorksheet.Range["J" + rowIndex + ":N" + rowIndex];
string resultFormat = "{0} to {4} units - {1}, <br /> {2} units or more - {3}";
string[] cellValues = discountInfoRange.ExistingCells.Select((c, index) => (index % 2 == 0) ? c.Value.ToString() : c.DisplayText).ToArray();
return Ok(string.Format(resultFormat, cellValues));
}
}
}
(function() {
var minRowIndex = 9, maxRowIndex = 17;
var currencyColumns = [3, 5, 6, 7],
discountColumns = [6],
weightColumns = [2];
var convertCurrencyCommand = { name: 'ConvertCurrency', text: 'Get Value In Euros', conversionRate: 0.87 },
discountCommand = { name: 'GetDiscountStructure', text: 'How the discount is calculated?' },
convertUnitsCommand = { name: 'ConvertUnits', text: 'Get Value In Lbs', conversionRate: 2.20462 };
function showPopoverAtElement(element, contentText) {
$('#popoverContainer').dxPopover('show', element);
$('#popoverContainer').dxPopover('instance').content().html('<div class="custom-content">' + contentText + '</div>');
$('#popoverContainer').dxPopover('repaint');
}
function SpreadsheetPopupMenuShowing(s, e) {
if(e.menuType === DevExpress.AspNetCore.Spreadsheet.PopupMenuType.Cell) {
e.menuItems.Clear();
if(needToAddCustomMenuItems(s)) {
if(isSelectedCellOfType(s, currencyColumns))
addCustomMenuItem(e.menuItems, convertCurrencyCommand);
if(isSelectedCellOfType(s, discountColumns))
addCustomMenuItem(e.menuItems, discountCommand);
if(isSelectedCellOfType(s, weightColumns))
addCustomMenuItem(e.menuItems, convertUnitsCommand);
}
}
else if(e.menuType !== DevExpress.AspNetCore.Spreadsheet.PopupMenuType.AutoFilter)
e.cancel = true;
}
function SpreadsheetCustomCommandExecuted(s, e) {
if(e.commandName === convertCurrencyCommand.name || e.commandName === convertUnitsCommand.name) {
var command = e.commandName === convertCurrencyCommand.name ? convertCurrencyCommand : convertUnitsCommand;
showPopoverAtElement(getActiveCellElement(s), convertActiveCellValue(s, command));
}
else if(e.commandName === discountCommand.name) {
getContextMenuCustomData(function(contentText) {
showPopoverAtElement(getActiveCellElement(s), contentText);
});
}
}
function needToAddCustomMenuItems(spreadsheet) {
var selection = spreadsheet.GetSelection();
return isSingleCellSelected(selection) && isSelectionWithinTable(selection) && spreadsheet.GetActiveCellValue() !== null;
}
function isSingleCellSelected(selection) {
return selection.leftColumnIndex === selection.rightColumnIndex && selection.topRowIndex === selection.bottomRowIndex;
}
function isSelectionWithinTable(selection) {
return selection.activeCellRowIndex >= minRowIndex && selection.activeCellRowIndex <= maxRowIndex;
}
function isSelectedCellOfType(spreadsheet, typedColumnsIndices) {
var selection = spreadsheet.GetSelection();
return typedColumnsIndices.indexOf(selection.activeCellColumnIndex) >= 0
}
function addCustomMenuItem(menuItems, command) {
var item = new DevExpress.AspNetCore.Spreadsheet.PopupMenuItem(command.name, command.text, null, null);
menuItems.Add(item);
}
function getActiveCellElement(spreadsheet) {
var renderHelper = spreadsheet.getRenderHelper();
return renderHelper.getActiveCellElement();
}
function convertActiveCellValue(spreadsheet, command) {
var convertedValue = convertNumericValue(getActiveCellNumericValue(spreadsheet), command);
if(command.name === convertUnitsCommand.name)
convertedValue = convertedValue + ' lbs';
else
convertedValue = '€' + convertedValue;
return convertedValue;
}
function getActiveCellNumericValue(spreadsheet) {
return spreadsheet.GetActiveCellValue().replace(/\$|(?:\skg)/g, '');
}
function convertNumericValue(value, conversionCommand) {
return Math.round(value * conversionCommand.conversionRate * 100) / 100;
}
window.SpreadsheetPopupMenuShowing = SpreadsheetPopupMenuShowing;
window.SpreadsheetCustomCommandExecuted = SpreadsheetCustomCommandExecuted;
})();