How to set DataRange for data validation from a different sheet using C#, VB.NET?
This article explains how to set a data range for data validation from a different sheet using ASP.NET Web Forms XlsIO.
What is Data Validation?
Data Validation is a list of rules for the data that can be entered in a cell. This can be applied by using the IDataValidation interface. The data range is the list of values that are to be displayed in the dropdown of data validation.
DataRange property of IDataValidation is used to set the data range for data validation.
Code snippet to set data range for data validation
// Initialize the data validation in first worksheet IDataValidation dataValidation = worksheet1.Range["E5"].DataValidation; // Assign data range for data validation from the second worksheet dataValidation.DataRange = worksheet2.UsedRange;
To know more about data validation, please refer documentation.
XlsIO throws an exception if the data range for data validation in one sheet is set from another sheet up to the release version 14.3.0.49. To avoid this exception please enable `Allow3DRangesInDataValidation` of IWorkbook.
Code snippet to avoid exception while setting data range for data validation from another worksheet
// Enable Allow3DRangesInDataValidation to avoid exception while setting the data range from another worksheet workbook.Allow3DRangesInDataValidation = true;
This has been resolved in the release version 14.3.0.52 (2016 Essential Studio Vol3 Service Pack – 1) rolled out in October 2016.
The following C#/VB complete code snippet illustrates how to set data range for data validation a from different worksheet.
C#
using Syncfusion.XlsIO; using System.IO; namespace XlsIO_DataValidation { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; // Creating a new workbook with three worksheets IWorkbook workbook = application.Workbooks.Create(3); // Access the first worksheet in the workbook IWorksheet worksheet1 = workbook.Worksheets[0]; //Access the second worksheet in the workbook IWorksheet worksheet2 = workbook.Worksheets[1]; // Enter values into a range of cells in second worksheet for (int i = 1; i <= 20; i++) { worksheet2.Range["A" + i.ToString()].Value = i.ToString(); } // Initialize data validation in first worksheet worksheet1.Range["E5"].Text = "Valiadtion"; worksheet1.UsedRange.AutofitColumns(); IDataValidation dataValidation = worksheet1.Range["E5"].DataValidation; // Assign data range for data validation from second worksheet dataValidation.DataRange = worksheet2.UsedRange; // Save the workbook as stream Stream stream = File.Create("Output.xlsx"); workbook.SaveAs(stream); } } } }
VB
Imports System.IO Imports Syncfusion.XlsIO Namespace XlsIO_DataValidation Class Program Public Shared Sub Main(ByVal args() As String) Using excelEngine As ExcelEngine = New ExcelEngine Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 'Creating a new workbook with three worksheets Dim workbook As IWorkbook = application.Workbooks.Create(3) 'Accessing first worksheet in the workbook Dim worksheet1 As IWorksheet = workbook.Worksheets(0) 'Accessing second worksheet in the workbook Dim worksheet2 As IWorksheet = workbook.Worksheets(1) 'Enter values into a range of cells in second worksheet Dim i As Integer = 1 Do While (i <= 20) worksheet2.Range(("A" + i.ToString)).Value = i.ToString i = (i + 1) Loop 'Initialize data validation in first worksheet worksheet1.Range("E5").Text = "Valiadtion" worksheet1.UsedRange.AutofitColumns() Dim dataValidation As IDataValidation = worksheet1.Range("E5").DataValidation 'Assign data range for data validation from second worksheet dataValidation.DataRange = worksheet2.UsedRange 'Save the workbook as stream Dim stream As Stream = File.Create("Output.xlsx") workbook.SaveAs(stream) End Using End Sub End Class End Namespace
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about How to set DataRange for data validation from a different sheet using C#, VB.NET.
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, Support Tickets, or feedback portal. We are always happy to assist you!