tags:

views:

35

answers:

1

Hey,

I have been experimenting with creating an Excel add in in C#. I was wondering if it is possible to programatically create a drop down list for a column where the items do not have to be in the worksheet? That is, I know it can be done using a data validation and a range of cells but I do not want the list of drop down items to be visible to the user/to be editable.

Any insight/links to aid in this would be greatly appreciated.

Thank you

A: 

You could use data validation, setting up the list in code, using something along these lines:

var worksheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
     var cell = (Range)worksheet.Cells[1, 1];
     cell.Validation.Add(
        XlDVType.xlValidateList, 
        XlDVAlertStyle.xlValidAlertStop, 
        XlFormatConditionOperator.xlBetween,
        "A, B, C, D, E");

This will create a data validation list with items A, B, C etc... These are still editable by the user through the validation menu, though.

Mathias
Thank you for your response. I tried this approach by creating a string with all my items on the fly. It works for a small number of items but throws an exception for a large number of items. Is there any way to work around this?
lotrij
How many items are we talking about? If you have a large number of items, a drop-down might not be the best approach anyways, because it becomes unpractical to use.
Mathias