|
| 1 | +--- |
| 2 | +title: Setting List DataValidation Rule to Reference Cell Range in SpreadProcessing |
| 3 | +description: Learn how to set up List Data Validation rules in Telerik Document Processing's SpreadProcessing library to reference a cell range instead of using a comma-delimited list. |
| 4 | +type: how-to |
| 5 | +page_title: Creating List Data Validation Rules Using Cell Ranges in SpreadProcessing |
| 6 | +meta_title: Creating List Data Validation Rules Using Cell Ranges in SpreadProcessing |
| 7 | +slug: spreadprocessing-list-data-validation-cell-range |
| 8 | +tags: spreadprocessing, list, data, validation, rule, cell, range, telerik, document, processing, libraries |
| 9 | +res_type: kb |
| 10 | +ticketid: 1695747 |
| 11 | +--- |
| 12 | + |
| 13 | +# Environment |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2025.3.806 | RadSpreadProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This article describes how to set a [List Data Validation]({%slug radspreadprocessing-features-data-validation%}#list-rule) rule in the [SpreadProcessing]({%slug radspreadprocessing-overview%}) library that uses a cell range as the validation source instead of a comma-delimited list of values. This avoids the 256-character limit imposed by Excel for comma-separated values. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to define validation rules with a cell range in SpreadProcessing? |
| 24 | +- How to bypass the character limit in validation rules by using cell ranges? |
| 25 | +- How to set up list validation using cell ranges in Telerik's SpreadProcessing? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To set a [List Data Validation]({%slug radspreadprocessing-features-data-validation%}#list-rule) rule that references a cell range, use the `ListDataValidationRule` and specify the cell range as the `Argument1`. Follow the steps below: |
| 30 | + |
| 31 | +1. Import the workbook using the [`XlsxFormatProvider`]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}). |
| 32 | +2. Specify the cell where the validation rule will apply using [`CellIndex`]({%slug radspreadprocessing-working-with-cells-accessing-cells-of-worksheet%}#accessing-cells-of-a-worksheet). |
| 33 | +3. Define the validation parameters, including the cell range for allowed values. |
| 34 | +4. Create and assign the [`ListDataValidationRule`]({%slug radspreadprocessing-features-data-validation%}#list-rule) to the target cell. |
| 35 | +5. Export the updated workbook using the [`XlsxFormatProvider`]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}). |
| 36 | + |
| 37 | +Here is an example: |
| 38 | + |
| 39 | +```csharp |
| 40 | +// Import the workbook |
| 41 | +Workbook workbook; |
| 42 | +IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider(); |
| 43 | + |
| 44 | +using (Stream input = new FileStream("input.xlsx", FileMode.Open)) |
| 45 | +{ |
| 46 | + workbook = xlsxFormatProvider.Import(input, TimeSpan.FromSeconds(10)); |
| 47 | +} |
| 48 | + |
| 49 | +// Access the worksheet |
| 50 | +var worksheet = workbook.Worksheets[0]; |
| 51 | + |
| 52 | +// Define the cell to apply validation |
| 53 | +CellIndex dataValidationRuleCellIndex = new CellIndex(4, 4); |
| 54 | + |
| 55 | +// Configure the validation rule context |
| 56 | +ListDataValidationRuleContext context = new ListDataValidationRuleContext(worksheet, dataValidationRuleCellIndex); |
| 57 | +context.InputMessageTitle = "Restricted input"; |
| 58 | +context.InputMessageContent = "The input is restricted to the week days."; |
| 59 | +context.ErrorStyle = ErrorStyle.Stop; |
| 60 | +context.ErrorAlertTitle = "Wrong value"; |
| 61 | +context.ErrorAlertContent = "The entered value is not valid. Allowed values are the week days!"; |
| 62 | +context.InCellDropdown = true; |
| 63 | + |
| 64 | +// Set the cell range as the validation source |
| 65 | +context.Argument1 = "=$A$1:$A$26"; |
| 66 | + |
| 67 | +// Create and apply the validation rule |
| 68 | +ListDataValidationRule rule = new ListDataValidationRule(context); |
| 69 | +worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule); |
| 70 | + |
| 71 | +// Export the workbook |
| 72 | +string xlsxOutputPath = "output.xlsx"; |
| 73 | +using (Stream output = new FileStream(xlsxOutputPath, FileMode.Create)) |
| 74 | +{ |
| 75 | + xlsxFormatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +# See Also |
| 80 | + |
| 81 | +* [Data Validation]({%slug radspreadprocessing-features-data-validation%}#data-validation) |
| 82 | +* [List Rule]({%slug radspreadprocessing-features-data-validation%}#list-rule) |
| 83 | +* [SpreadProcessing Overview]({%slug radspreadprocessing-overview%}) |
0 commit comments