1

Can we render two UI element at same grid position in SelectionChanged event of a combo box.

The exact problem is i have grid and inside the grid there are two rows the first row will always show me a ComboBox(of which i was talking about line before) on selecting different item will render different UI elemnts at same position in the second row of the grid.

So i tried to do the rendering of the UI element in the SelectionChanged event of the Combo.

But the problem happening is when i select the first element then it is displayed properly but when i try to select the second UI element to be rendered at same position on Combo selection change then it gives a warning "the value is not within the expected range." at Line : "bigGrid.Children.Add(ck);"

My code to do this is:

Grid childGrid = createChildGrid();
Grid ck = new Grid(); //This ck will contain ComboBox at row zero and UI element at second row
ck.RowDefinitions.Add(new RowDefinition());
ck.RowDefinitions.Add(new RowDefinition());
int loopCount = 0; //This variable will help in changing the row index

if (loopCount == 0) 
{
    ComboBox cmb = new ComboBox();
    //Here i add items in Combo:
    foreach(string ccyp in attributeName)
    {
        if (ccyp != null)
            cmb.Items.Add(ccyp);
    }
    cmb.SelectionChanged += (o, e) =>
      {
        txtblkName.Text = cmb.SelectedValue.ToString();
        ck = generateUIElementCorrespondingTotxtLabelNameObtainedOnComboSelection(txtblkName.Text); //this function will return a grid containing the selected UI elemnt from combo box.
        Grid.SetRow(ck, loopCount);
        bigGrid.Children.Add(ck); //**THIS LINE GIVE EXCEPTION: "the value is not within the expected range."**

    };
}
Grid.SetColumn(cmb, 1);
childGrid.Children.Add(cmb);
Grid.SetRow(childGrid, loopCount);
bigGrid.Children.Add(childGrid);
loopCount++;// I incrtease loop count so that on selection changed event of combo UI will be displayed on second row loopcount=1 

EDIT: On debugging i get :The result of break point is : loopcount=1 (for rendering of combo item in second row)this " Grid.SetRow(ck, loopCount); bigGrid.Children.Add(ck);" and for combo box display loopcount= 0 (i mean here : "Grid.SetRow(childGrid, loopCount); bigGrid.Children.Add(childGrid); loopCount++;")

user3735822
  • 337
  • 2
  • 12
  • It is more likely that it is the line above `Grid.SetRow` that causes the error where you are quite likely trying to set a row where the count is too high – Sayse Aug 01 '14 at 07:47
  • @Sayse By two hight . What does it mean ? does that mean that : I have done ck.RowDefinitions.Add(new RowDefinition()); less number of times then expected? – user3735822 Aug 01 '14 at 07:48
  • Or does that mean i already have a child at this loopcount position ? – user3735822 Aug 01 '14 at 07:51
  • Its not really possible to help anymore other than to say set breakpoints, it could be that `ck` isn't a framework element within the grid – Sayse Aug 01 '14 at 07:52
  • Framework element within the grid means ? – user3735822 Aug 01 '14 at 07:54
  • [Grid.SetRow](http://msdn.microsoft.com/en-us/library/cc190424(v=vs.95).aspx) - Set breakpoints and add watches – Sayse Aug 01 '14 at 07:55
  • @Sayse I already doen that. In fact its the first thing i did after i am here. The result of break point is : loopcount value=1 for this " Grid.SetRow(ck, loopCount); bigGrid.Children.Add(ck);" and for combo box display loopc ount= 0 (i mean here : "Grid.SetRow(childGrid, loopCount); bigGrid.Children.Add(childGrid); loopCount++;") – user3735822 Aug 01 '14 at 08:10
  • 1
    In VisualStudio the highlighted line when the debugger breaks because an exception ocurred is in my experience NOT the line that caused it, look at the previous line and you have your culprit. – Martin Aug 01 '14 at 08:48
  • @Martin previous line is : Grid.SetRow(ck, loopCount); – user3735822 Aug 01 '14 at 08:53
  • @Martin please see the edit part of thequestion what i get on debugging – user3735822 Aug 01 '14 at 08:58
  • Put a breakpoint in your SelectionChanged handler, and make sure it isn't executing multiple times for a single 'change' – Mashton Aug 01 '14 at 11:14

1 Answers1

1

From question Silverlight: Value does not fall within the expected range exception

'This error can be caused when there are two elements being added with the same name.' What identifiers are added to the combo boxes?

If this is the problem, as suggested in the linked question, you could add a unique identifier to each combo box.

Community
  • 1
  • 1
Patrick Developer
  • 369
  • 2
  • 7
  • 21
  • MY COMBO BOX contains the attribute name (which i get from deserialisation of same xml (xml in class objects) thats true that both the added elemnts in combo are from same class). How can we give unique identifier to the combo items ? – user3735822 Aug 01 '14 at 08:52
  • This [link](http://stackoverflow.com/questions/3762981/hidden-id-with-combobox-items) shows how to set up a class to override the ToString() method, and add a hidden value to your combo box items. – Patrick Developer Aug 01 '14 at 09:09
  • No i do not htinkthatthis was a problem i have implemented the data on thta link but same problem – user3735822 Aug 01 '14 at 09:28
  • What does the generateComboBox() method do? – Patrick Developer Aug 01 '14 at 09:55
  • please see the edit . It is not necessarily ComboBoc generation. It could be any UI element (May be generateList(..)). I wil select in combo box that which UI element must be displayed in row below and on selection i pass as parameter that text name and in function definition i create that UI element (It may be "Combo" or "List" or "RadioButton" in combo item options-which dépends upon user to select). – user3735822 Aug 01 '14 at 10:04
  • see in edited question. This generateUIElementCorrespondingTotxtLabelNameObtainedOnComboSelection() will return the grid containign that corresponding selected UI element which is then displayed in row below. On the first UI element selection from combo it works fine but when i select another UI element (which is supposed to be replce the existing UI element) then it gives that exception. – user3735822 Aug 01 '14 at 10:07
  • I was just wondering if the UI element that replaces the existing element in the thegenerateUIElementCorrespondingTotxtLabelNameObtainedOnComboSelection() method is added with the same name as the previous element. What does the breakpoint tell you at ck = generateUIElementCorrespondingTotxtLabelNameObtainedOnComboSelection(txtblkName.Text) method, what is different between the first and second loops? – Patrick Developer Aug 01 '14 at 10:20