To make is easier to understand information contained in a worksheet and refer to individual cells, cell ranges, formulas and constant values, you can use defined names. A defined name is an object that implements the DefinedName interface. Its DefinedName.Name property specifies the name that refers to a cell, cell range, formula or constant specified by the DefinedName.RefersTo property.
You can provide a defined name with an explanation or additional information via the DefinedName.Comment property. To get or set a cell range associated with the defined name, use the DefinedName.Range property.
Each defined name has a scope - an area (an individual worksheet or entire workbook) within which a name is recognized and can be used without qualification. So, each worksheet contained in the workbook and the entire workbook has it’s own collection of defined names (DefinedNameCollection) that can be accessed via the Worksheet.DefinedNames or IWorkbook.DefinedNames property, respectively. You can get a defined name object by its index in the collection or by its name (DefinedNameCollection.GetDefinedName).
For more information on defined names, see the Defined Names document.
- Use the Worksheet.Range property to access a cell range (see the How to: Access a Range of Cells document for details). Then, specify the range name via the CellRange.Name property of the created CellRange object.
- Use the DefinedNameCollection.Add method to add a new defined name for the specified range to the worksheet’s collection of defined names (Worksheet.DefinedNames). Then, use the Worksheet.Range property and pass the DefinedName.Name property value of the previously created defined name object.
When specifying a name for a cell or range of cells, follow the rules listed in the Defined Names document.
using DevExpress.Spreadsheet; // ... IWorkbook workbook = spreadsheetControl1.Document; Worksheet worksheet = workbook.Worksheets; // Create a range. CellRange rangeA2A4 = worksheet.Range["A2:A4"]; // Specify the name for the created range. rangeA2A4.Name = "rangeA2A4"; // Create a new defined name with the specifed range name and absolute reference. DefinedName definedName = worksheet.DefinedNames.Add("rangeC2D3", "Sheet1!$C$2:$D$3"); // Create a range using the specified defined name. CellRange rangeC2D3 = worksheet.Range[definedName.Name];
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the DefinedName interface.
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.