What event to I need to handle to allow users to add "fruit" the either a valuelist or ultra dropdown.
Since it is a KVP I always get format exception
//Create some fruitDictionary<int,string> fruits = new Dictionary<int,string>();fruits.Add(-1,"apple");fruits.Add(-2,"banana");//Create and add to the ultraDropDownUltraDropDown fruitUltraDropDown = new UltraDropDown();fruitUltraDropDown.DataSource = fruits.ToList();fruitUltraDropDown.DisplayMember = "Value";fruitUltraDropDown.ValueMember = "Key";MyUltraGrid.DisplayLayout.Bands[0].Columns["MyColumn"].ValueList = fruitUltraDropDown;
What event can I handle so when a user types "grape" I can add it to the dictionary with my own key, and it gets added to the dropdownlist. Currently if I type "grape in the cell, I just get a format exception.
Regards
_Eric
Hi Eric,
There are a number of events you could use. I would probably use BeforeCellUpdate or maybe BeforeExitEditMode.
Either way, what you would do is use the ValueListResolved property on the cell to get the ValueList and then you can use the GetValue method to try to find a matching item on the list. Use the cell.EditorResolved.Text to get the current edit text in the cell for your search.
Thanks Mike, almost there now, I could not use BeforeCellUpdate as it would always throw the exception before that would fire. Now I am unsure how to set the value of cell's dropdown index to the newly added item
/// <summary> /// Handles the BeforeExitEditMode event of the ultraGrid2 control. All our work should go here to add to the list /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs"/> instance containing the event data.</param> private void ultraGrid2_BeforeExitEditMode(object sender, Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs e) { Console.WriteLine("ultraGrid2_BeforeExitEditMode: sender {0}", sender); UltraGrid grid = sender as UltraGrid; //What cell raised this event? var cell = grid.ActiveCell; if (cell.Column.Index == 1)//This is the fruit ID { //Cast to the IValueList IValueList valueList = cell.ValueListResolved; int index = 0; if (valueList.GetValue(cell.Text, ref index) != null) { Console.WriteLine("Already in list, nothing to do"); } else { Console.WriteLine("NOt in list"); //Lets add it to the backer fruits.Add(new Fruit(cell.Text)); Console.WriteLine("Adding {0} to fruits.", cell.Text); fruitListUltraDropDown.DataSource = null; fruitListUltraDropDown.DataSource = fruits; fruitListUltraDropDown.DataBind(); //How to set it to the newly added item in the value list } } }
It still throws an exception cannot convert "grape" to int.32. The datasource/Valuelist contain the new value but I am unsure how to set the cells valueListiIndex to the new item I just added.