-
Notifications
You must be signed in to change notification settings - Fork 41
Added new kb article adding-combobox-to-excel-file-radspreadprocessing #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||
| title: Adding a DropDown/ComboBox to an Excel File Using RadSpreadProcessing | ||||||||||||||||||||||||||||||||||
| description: Explains how to add a combo box with specific options to an Excel file using RadSpreadProcessing. | ||||||||||||||||||||||||||||||||||
| type: how-to | ||||||||||||||||||||||||||||||||||
| page_title: Adding a /ComboBox in Excel Using RadSpreadProcessing | ||||||||||||||||||||||||||||||||||
| slug: adding-combobox-to-excel-file-radspreadprocessing | ||||||||||||||||||||||||||||||||||
| tags: spread, processing, combobox, excel, listdata, validation, document, dropdown, comma, delimiter | ||||||||||||||||||||||||||||||||||
| res_type: kb | ||||||||||||||||||||||||||||||||||
| ticketid: 1689410 | ||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Environment | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| | Version | Product | Author | | ||||||||||||||||||||||||||||||||||
| | ---- | ---- | ---- | | ||||||||||||||||||||||||||||||||||
| | 2025.2.520| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Description | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Learn how to add a ComboBox with predefined options to an Excel file programmatically using [RadSpreadProcessing]({%slug radspreadprocessing-overview%}). Additionally, the ComboBox should support special characters like commas (or another delimiter) within the options. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Solution | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| To add a DropDown/ComboBox to an Excel file and handle special characters like commas within options, use the [ListDataValidationRule]({%slug radspreadprocessing-features-data-validation%}) with a cell range as the source for the validation. Consider the following sample options: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| * Option 1, Inc. | ||||||||||||||||||||||||||||||||||
| * Option 2, Inc. | ||||||||||||||||||||||||||||||||||
| * Option 3, Inc. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Below is the sample code: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```csharp | ||||||||||||||||||||||||||||||||||
| // Create a workbook | ||||||||||||||||||||||||||||||||||
| Workbook workbook = new Workbook(); | ||||||||||||||||||||||||||||||||||
| Worksheet worksheet = workbook.Worksheets.Add(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Define the ComboBox options in a cell range | ||||||||||||||||||||||||||||||||||
| worksheet.Cells[0, 0].SetValue("ABC, Inc."); | ||||||||||||||||||||||||||||||||||
| worksheet.Cells[0, 1].SetValue("123, Inc."); | ||||||||||||||||||||||||||||||||||
| worksheet.Cells[0, 2].SetValue("KEF, Inc."); | ||||||||||||||||||||||||||||||||||
| worksheet.Cells[0, 3].SetValue("HMS, Inc."); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Specify the cell where the ComboBox will be applied (e.g., A2) | ||||||||||||||||||||||||||||||||||
| CellIndex cellIndex = new CellIndex(1, 0); // A2 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Set up the validation rule with the cell range | ||||||||||||||||||||||||||||||||||
| ListDataValidationRuleContext context = new ListDataValidationRuleContext(worksheet, cellIndex); | ||||||||||||||||||||||||||||||||||
| context.InputMessageTitle = "Restricted input"; | ||||||||||||||||||||||||||||||||||
| context.InputMessageContent = "Please select an option from the dropdown."; | ||||||||||||||||||||||||||||||||||
| context.ErrorStyle = ErrorStyle.Stop; | ||||||||||||||||||||||||||||||||||
| context.ErrorAlertTitle = "Invalid Input"; | ||||||||||||||||||||||||||||||||||
| context.ErrorAlertContent = "The entered value is not valid. Allowed values are listed in the dropdown."; | ||||||||||||||||||||||||||||||||||
| context.Argument1 = "=A1:D1"; // Cell range containing the options | ||||||||||||||||||||||||||||||||||
| ListDataValidationRule rule = new ListDataValidationRule(context); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Apply the validation rule to the specified cell | ||||||||||||||||||||||||||||||||||
| worksheet.Cells[cellIndex].SetDataValidationRule(rule); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Save the workbook to a file | ||||||||||||||||||||||||||||||||||
| string outputFilePath = "ComboBoxExample.xlsx"; | ||||||||||||||||||||||||||||||||||
| File.Delete(outputFilePath); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| File.Delete(outputFilePath); | |
| if (File.Exists(outputFilePath)) | |
| { | |
| try | |
| { | |
| File.Delete(outputFilePath); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine($"Failed to delete the file: {ex.Message}"); | |
| } | |
| } |
Copilot
AI
Jun 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding error handling or additional configuration options when starting a process to ensure robustness, especially in environments with strict security policies.
| Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); | |
| if (File.Exists(outputFilePath)) | |
| { | |
| try | |
| { | |
| Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine($"An error occurred while trying to open the file: {ex.Message}"); | |
| } | |
| } | |
| else | |
| { | |
| Console.WriteLine("The file does not exist: " + outputFilePath); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.