How to remove used items from combobox cell type in grid ?

Posted by: manish-tewary on 8 September 2017, 2:54 pm EST

  • Posted 8 September 2017, 2:54 pm EST

    I have a grid with a combobx cell type .


    All  I want is that when i user click on the dropdown to select any item, the items which are already selected should not come in the current row. Also, if the user changes any of the previous defined values then dropdown should be updated accordingly.


    e.g, lets say dropdown has 4 item: "A", "B", "C" and "D".


    For the first row if the user selects item "A", then when it clicks on the dropdown in the subsequent rows item "A" should not appear in the list. Now suppose the user selects "B" for the second row and then immeditely jumps to dropdown at the first row then both the items "A" and "B" should not be there.


     

  • Replied 8 September 2017, 2:54 pm EST

    wasn't easy, but this does it.

    <span style="color:blue;font-family:Consolas;font-size:12pt;">namespace</span><span style="font-family:Consolas;font-size:12pt;"> WindowsFormsApplication23</span>

    <span style="font-family:Consolas;font-size:12pt;">{</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">    </span><span style="color:blue;">public</span> <span style="color:blue;">partial</span> <span style="color:blue;">class</span> <span>Form1</span> : <span>Form</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">    </span>{</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span><span style="color:blue;">public</span>
    Form1()</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>{</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>InitializeComponent();</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>}</span>

    <span style="font-family:Consolas;font-size:12pt;"></span> 

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span><span style="color:blue;">private</span>
    <span style="color:blue;">void</span> Form1_Load(<span style="color:blue;">object</span>
    sender, <span>EventArgs</span> e)</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>{</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:blue;">string</span>[]
    array = { <span>"A"</span>, <span>"B"</span>, <span>"C"</span>,
    <span>"D"</span> };</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span> cmbx = <span style="color:blue;">new</span>
    FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span>();</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>cmbx.Items = array;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">  
    </span><span style="mso-spacerun:yes;">         </span>fpSpread1.ActiveSheet.Columns[1].CellType
    = cmbx;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>}</span>

    <span style="font-family:Consolas;font-size:12pt;"></span> 

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span><span style="color:blue;">private</span>
    <span style="color:blue;">void</span> fpSpread1_ComboCloseUp(<span style="color:blue;">object</span> sender, FarPoint.Win.Spread.<span>EditorNotifyEventArgs</span> e)</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>{</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Get a
    reference to the CellType</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span> cbct =</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">                </span>(FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span>)fpSpread1.ActiveSheet.Columns[e.Column].CellType;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Instantiate
    an ArrayList for ease of array operations</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">          </span><span style="mso-spacerun:yes;">  </span>System.Collections.<span>ArrayList</span>
    list = <span style="color:blue;">new</span> System.Collections.<span>ArrayList</span>();</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Add the
    combo Items to the ArrayList</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:blue;">for</span>
    (<span style="color:blue;">int</span> i = 0; i < cbct.Items.Length; i++)</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">                </span>list.Add(cbct.Items[i ]);</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Get the
    selected object</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:blue;">object</span>
    selection = fpSpread1.ActiveSheet.Cells[e.Row, e.Column].Value;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Remove
    selection from ArrayList</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>list.Remove(selection);</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//create
    a string array from the arraylist</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:blue;">string</span>[]
    array = list.ToArray(<span style="color:blue;">typeof</span>(<span style="color:blue;">string</span>)) <span style="color:blue;">as</span> <span style="color:blue;">string</span>[];</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span><span style="color:green;">//Instantiate
    a new combobox celltype</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span> cmbx = <span style="color:blue;">new</span>
    FarPoint.Win.Spread.CellType.<span>ComboBoxCellType</span>();</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">            </span>cmbx.Items = array;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">  
    </span><span style="mso-spacerun:yes;">         </span><span style="color:green;">//assign
    to the column</span></span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">           
    </span>fpSpread1.ActiveSheet.Columns[1].CellType = cmbx;</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">        </span>}</span>

    <span style="font-family:Consolas;font-size:12pt;"><span style="mso-spacerun:yes;">    </span>}<span style="mso-spacerun:yes;">                    </span></span>

    <span style="font-family:Consolas;font-size:12pt;">}</span>

    <font size="3" face="Times New Roman">

    </font>

  • Replied 8 September 2017, 2:54 pm EST

    Hi,


    above code is not working properly.


    <SPAN style="FONT-FAMILY:'Arial','sans-serif';FONT-SIZE:10pt;">You have removed the currently selected object for the array list and then assigning this array to the combobox celltype then how come the currently selected object will be shown in the current combobox.<o:p></o:p></SPAN>


    <SPAN style="FONT-FAMILY:'Arial','sans-serif';FONT-SIZE:10pt;">Also, this will not work for multiple rows. Kindly check<o:p></o:p></SPAN>


     

  • Replied 8 September 2017, 2:54 pm EST

    Hello,

    I would like you to know that what you are seeing is the intended behavior, once you remove an item from the drop down list of the ComboBox it would no more show up as a value in the ComboBox cell as the value no longer exists in the list. So, the only workaround possible would be to display, the current selected value in the cell, along with the remaining items that are not selected, whenever the ComboBox list is dropped down.

    I have attached a sample that depicts the same, kindly have a look at it, hope it will help you. Please let me know if you have any queries further.

    Thanks,

    Manpreet Kaur


    2012/03/combodropdown.zip
Need extra support?

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

Learn More

Forum Channels