Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have Problem with delete row from wpf grid c#.

I want to delete row,
int indx = Grid.GetRow(sender as Button);
int row = layOut.RowDefinitions.Count;
List<UIElement> elementsToRemove = new List<UIElement>();
foreach (UIElement element in layOut.Children)
{
    var d = Grid.GetRow(element);
    if (Grid.GetRow(element) == Grid.GetRow(sender as Button))
        elementsToRemove.Add(element);
}
foreach (UIElement element in elementsToRemove)
    layOut.Children.Remove(element);


layOut.RowDefinitions.RemoveAt(indx-1);

Error:
Specified argument was out of the range of valid values.
Parameter name: Index is out of collection's boundary.
Posted
Updated 13-Feb-12 20:42pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Feb-12 2:16am    
Did you run it under debugger?
--SA
Rajeev Jayaram 14-Feb-12 2:43am    
[Edit] - Code block added.

1 solution

I bet indx-1 is less than zero. After all, always run the code under debugger before asking questions like that.

—SA
 
Share this answer
 
Comments
manoj12.shrivastava 6-Apr-12 7:18am    
sir my problem is only that when i m trying to delete row from grid it is working fine.

suppose i have 3 rows in wpf grid
if i delete first row it is deleting fine row1
if i delete second row it is deleting fine row2
but when i delete last row then problem is occured.

sir if you can help me then pls...
Sergey Alexandrovich Kryukov 8-Apr-12 0:45am    
Well, do you understand that everything is normally indexed from 0 to count-1, not from 1 to count? That could be your problem.
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900