Log in to like this post! WinGrid Dropdown Column to Excel Dropdown List Tom Puglisi / Tuesday, November 22, 2011 Ever wanted to export your WinGrid to a Microsoft™ Excel™ file complete with your dropdown columns intact so that your end users could select items from the same list in Excel? Well with the new Infragistics Excel Library ListDataValidationRule feature, part of the Excel Data Validation feature set, YOU CAN! How this is done: The sample application contains a WinGrid control bound to the Products table in the Northwind database. The WinGrid’s CategoryID column is wired up to the WinDropdown control, which essentially provides a dropdown list of values for the CategoryID column. The WinDropdown control is bound to the Category table in the Northwind database. End users can change the value of the CategoryID column by picking from the list of values available from the WinDropdown control. We use the WinGridExcelExporter component to easily export WinGrid into an Excel file. The goal of this blog post is to provide our end users with the same experience within the Excel file that we export with WinGridExcelExporter – a dropdown list of values on the CategoryID column so that end users can pick from the same list of values available in WinGrid. The following class wraps up the logic used to create a ListValidationRule object and apply it to each WorkBookCell object. view plaincopy to clipboardprint using System; using System.Collections.Generic; using System.Linq; using System.Text; using Infragistics.Documents.Excel; using Infragistics.Win.UltraWinGrid.ExcelExport; namespace NewExcelFeatures { public class ExcelListConverter { private UltraGridExcelExporter _theExcelExporter; private string _theItemsListString; private string _theColumnName; private Dictionary<int, string> CellReferenceDictionary = new Dictionary<int, string>(); public ExcelListConverter(UltraGridExcelExporter theExcelExporter, string theItemsListString, string theColumnName) { _theExcelExporter = theExcelExporter; _theItemsListString = theItemsListString; _theColumnName = theColumnName; _theExcelExporter.CellExported += new CellExportedEventHandler(_theExcelExporter_CellExported); //Used to convert a column index into an Excel Cell Column Address Reference Character CellReferenceDictionary.Add(0, "A"); CellReferenceDictionary.Add(1, "B"); CellReferenceDictionary.Add(2, "C"); CellReferenceDictionary.Add(3, "D"); CellReferenceDictionary.Add(4, "E"); CellReferenceDictionary.Add(5, "F"); CellReferenceDictionary.Add(6, "G"); CellReferenceDictionary.Add(7, "H"); CellReferenceDictionary.Add(8, "I"); } /// <summary> /// While each cell is exported, we add a list validation rule to the appropriate Excel Column /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _theExcelExporter_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e) { if (e.GridColumn.Key == _theColumnName) { WorksheetCell c = e.CurrentWorksheet.Rows[e.CurrentRowIndex].Cells[e.CurrentColumnIndex] as WorksheetCell; string currentRowIndex = (e.CurrentRowIndex + 1).ToString(); this.SetListValidationRule(e.CurrentWorksheet, _theItemsListString, CellReferenceDictionary[e.CurrentColumnIndex] + currentRowIndex); } } private void SetListValidationRule(Worksheet theWorksheet, string theListItems, string theCellAddress) { // 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 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 cell's valid values string theListOfItemsExcelString = string.Format("=\"{0}\"", theListItems); ld.SetValuesFormula(theListOfItemsExcelString, theCellAddress); // Reference the worksheet cell in the collection cellCollection = new WorksheetReferenceCollection(theWorksheet, theCellAddress); // 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 theWorksheet.DataValidationRules.Add(dataRule, cellCollection); } } } A simple example of using this class is shown here, in the button_click event handler found on the application’s main form: view plaincopy to clipboardprint private void btnExport_Click(object sender, EventArgs e) { string validationList = string.Empty; //create a comma delimited list of values (no crazy chars allowed!) foreach (DataRow d in this.nwindDataSet.Categories.Rows) { validationList += d["CategoryName"].ToString() + ","; } validationList = validationList.TrimEnd(new char[] { ',' }); ExcelListConverter c = new ExcelListConverter(this.ultraGridExcelExporter1, validationList, "CategoryID"); string theFile = Application.StartupPath + @"\theFile.xls"; this.ultraGridExcelExporter1.Export(this.ultraGrid1, theFile); System.Diagnostics.Process.Start(theFile); c = null; } Now your end users can select from the same list of items in your newly generated Excel file! You can download the sample application attached to this blog post. NewExcelFeatures.zip