tags:

views:

13

answers:

2

Hello,

I have a Grid control (not a DataGrid) and I want to remove the content of e.g. the second column in the second row. Can this be done without having a reference to the content of a grid cell?

Thank you very much for any hint!

A: 

In codebehind, I suppose?

If they are declared in order and all fields are filled, you can get the number of the cell by

int cellNumber = rowNumber * columnCount + columnNumber;

Then myGrid.Children[cellNumber-1] gives you the child node you want.

myGrid.Children.RemoveAt(ellNumber-1); would remove that child node from the grid.

Note though that it only gets removed from the grid's list of children. If you have any other references to that item, you must take care of them, too.

Martin
Sorry, I just realize that my answer doesn't really solve your problem. This solution only works if all cells are declared in the order left-to-right->top-to-bottom.
Martin
+1  A: 

Found a more stable solution, though it requires looping through the cells:

        // these are the row and column number of the cell
        // you want to have removed...
        int getRow = 2, getCol = 5;
        for (int i = 0; i < myGrid.Children.Count; i++)
            if ((Grid.GetRow(myGrid.Children[i]) == getRow)
                && (Grid.GetColumn(myGrid.Children[i]) == getCol))
            {
                myGrid.Children.Remove(myGrid.Children[i]);
                break;
            }
Martin
Thank you very much, shame on me I didn't get this. Was desperately looking for sth. like grid[2][5] ;-)
stefan.at.wpf