// Create the workbook with one worksheet called Validation
Infragistics.Documents.Excel.Workbook workbook = new Infragistics.Documents.Excel.Workbook();
Infragistics.Documents.Excel.Worksheet worksheet = workbook.Worksheets.Add("Validation");
// Create a variable for the base data validation rule
DataValidationRule dataRule = null;
// Create a variable for the worksheet reference collection
WorksheetReferenceCollection cellCollection = null;
// Create a variable for a worksheet cell
WorksheetCell wsc = null;
// Create a new list data validation rule
ListDataValidationRule ld = new ListDataValidationRule();
// Copy the validation rule reference to the base data rule variable
dataRule = ld;
// Allow the worksheet cell to contain null values
ld.AllowNull = true;
// Allow the showing of the drop down for valid cell values
ld.ShowDropdown = true;
// Set the A1 cell's valid values
ld.SetValuesFormula("=\"cat, dog, bird, goat, sheep, turtle, horse, chicken\"", "A1");
// Reference the worksheet cell in the collection
cellCollection = new WorksheetReferenceCollection(worksheet, "A1");
// Add the error message information
dataRule.ErrorMessageDescription = "Invalid value entered.";
dataRule.ErrorMessageTitle = "Validation Error";
dataRule.ErrorStyle = DataValidationErrorStyle.Stop;
// Add the input message information
dataRule.InputMessageDescription = "Type or select a value from the list.";
dataRule.InputMessageTitle = "Value Selection";
// Set whether or not to display the error message after an invalid value was entered
dataRule.ShowErrorMessageForInvalidValue = true;
// Set whether or not to display the input message
dataRule.ShowInputMessage = true;
// Add the data validation to the worksheet
worksheet.DataValidationRules.Add(dataRule, cellCollection);
try
{
// Save the created workbook
workbook.Save("output.xls");
// Open the workbook to display the results
System.Diagnostics.Process.Start("output.xls");
}
catch
{
MessageBox.Show("Please close any open instances of Excel.",
"Error Opening Workbook",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}