Dropdown list in Spreadsheet cell

Posted by: ravi9004 on 10 September 2017, 10:57 am EST

  • Posted 10 September 2017, 10:57 am EST

    Hello,

    Any one can tell me how to add  dropdown list in Spreadsheet cell.

     Thanks

  • Replied 10 September 2017, 10:57 am EST

    Hello,

    You may use the code as follows to create a custom drawing object with a silverlight combobox.

    public MainPage()
            {
                InitializeComponent();
                this.gcSpreadSheet1.Sheets.Clear();
                this.gcSpreadSheet1.Sheets.Add(new MyWorksheet());
                var sheet = this.gcSpreadSheet1.ActiveSheet;          
            }
        }
        public class ControlDrawingObject : CustomDrawingObject
        {
            private Control _rootElement;
            public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }
            public override FrameworkElement RootElement
            {
                get { _rootElement.Margin = new Thickness(1); return _rootElement; }
            }
        }

        public class MyWorksheet : Worksheet
        {
            public bool DrawingObjectVisible { get; set; }

            public override DrawingObject[] GetDrawingObject(int row, int column, int rowCount, int columnCount)
            {
                if (row != 1 || column != 1) return base.GetDrawingObject(row, column, rowCount, columnCount);

              
               
                ComboBox cmb = new ComboBox();
                cmb.Items.Add("One");
                cmb.Items.Add("Two");
                cmb.Items.Add("Three"); 
                DrawingObject dobj;
                dobj = new ControlDrawingObject(row, column, cmb);
                return new DrawingObject[] { dobj };

            }

        }

     

    Thanks,

  • Replied 10 September 2017, 10:57 am EST

    If you need a simple dropdown list, maybe you can use DataValidator to add a list validator to a cell, a dropdown button when show when the cell is active. Code may like this:

                sheet[1, 3].DataValidator = DataValidator.CreateListValidator("item1,item2,item3"); 

  • Replied 10 September 2017, 10:57 am EST

    Hi,

    I was trying to use this code for various controls (ComboBox, CheckBox, ImagePicker, ...) and it shows what I need visually. However, how can I get the value back from the control to the cell. If a user picks a new value from the ComboBox, I'll need to update the value.

    I tried adding a eventhandler to the SelectionChanged event of the ComboBox, but I lose the cell value that way. We would have 5 ComBoxes with a different list of values and cell are added dynamically. So we can't really check the indexes of the row/column to see what the DrawingObject should return. The only option I saw was to place all the info in the Tag of the cell (type of control, list o items, ...), but this get's bloated fast and is a pretty ugly way of working. I tried inheriting Cell, hoping I could add some custom properties to make this job a bit easier, but it's NotInheritable.

    So my question is: the above way for adding a control isn't a problem to display them, but how do you get the values (SelectedValue, IsChecked, ...) from the cell after the EditEnd (well, EditEnd doesn't even fire if you change the ComboBox value or check the CheckBox...)

     

    Thanks in advance,

    S.W.

     

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels