How to create list data validation for more than 255 characters in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files.
The limit of the ListOfValues property, which is used to assign values in the List-type Data Validation, is only 255 characters, including separators. It is not the number of values that is counted, but the length of characters in each value is counted.
Example: If you are assigning the ListOfValues property with `new string[] { "ListItem1", "ListItem2", "ListItem3" }`, then the character count is 29.
However, it is possible to populate more than 255 characters into List-type Data Validation through different methods. These methods are:
1. Creating a named range for the values and assigning that named range to the FirstFormula property.
C#
IName workBookName = workbook.Names.Add("Numbers"); workBookName.RefersToRange = worksheet.UsedRange; // Data Validation for List IDataValidation listValidation = worksheet.Range["C3"].DataValidation; worksheet.Range["C1"].Text = "Data Validation List in C3"; worksheet.Range["C1"].AutofitColumns(); listValidation.ListOfValues = new string[] { }; listValidation.FirstFormula = "=Numbers";
2. Directly assigning the worksheet range containing values to the FirstFormula property.
C#
// Data Validation for List IDataValidation listValidation = worksheet.Range["C3"].DataValidation; worksheet.Range["C1"].Text = "Data Validation List in C3"; worksheet.Range["C1"].AutofitColumns(); listValidation.ListOfValues = new string[] { }; listValidation.FirstFormula = "=A1:A600";
The following complete code snippet explains how to create a List-type Data Validation for more than 255 characters through a named range.
C#
using Syncfusion.XlsIO; namespace I342696_Console { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Open("../../Sample.xlsx"); IWorksheet worksheet = workbook.Worksheets[0]; IName workBookName = workbook.Names.Add("Numbers"); workBookName.RefersToRange = worksheet.UsedRange; //Data Validation for List IDataValidation listValidation = worksheet.Range["C3"].DataValidation; worksheet.Range["C1"].Text = "Data Validation List in C3"; worksheet.Range["C1"].AutofitColumns(); listValidation.ListOfValues = new string[] { }; listValidation.FirstFormula = "=Numbers"; //Shows the error message listValidation.ErrorBoxText = "Choose the value from the list"; listValidation.ErrorBoxTitle = "ERROR"; listValidation.PromptBoxText = "Data validation for list"; listValidation.IsPromptBoxVisible = true; listValidation.ShowPromptBox = true; workbook.SaveAs("DataValidation.xlsx"); System.Diagnostics.Process.Start("DataValidation.xlsx"); } } } }
A working sample can be downloaded from ListDataValidation.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheets using Template Markers, and most importantly, PDF and image conversions, etc., with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to create list data validation for more than 255 characters in C#, VB.NET in XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!