Articles in this section
Category / Section

How to set DataRange for data validation from a different sheet using C#, VB.NET?

2 mins read

This article explains how to set 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 to the data that can be entered in a cell. This can be applied by using IDataValidation interface. 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 data validation in first worksheet
IDataValidation dataValidation = worksheet1.Range["E5"].DataValidation;
 
//Assign data range for data validation from second worksheet
dataValidation.DataRange = worksheet2.UsedRange;

Download complete sample

To know more about data validation, please refer documentation.

Note:

XlsIO throws 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 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);
 
  //Accessing first worksheet in the workbook
  IWorksheet worksheet1 = workbook.Worksheets[0];
 
  //Accessing 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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied