Tvd
Tvd

Reputation: 4601

Unexpected results on deleteing a row from Grid

I have a Grid whose Rows & cols are populated dynamically. To delete a row I use the following code :

seivesTorGrid.RowDefinitions.RemoveAt(rowIndex+1);

Everything is done manually only. The results of the operations after deleting the row is not satisfactory : enter image description here

Defination of Grid in xml is :

<Grid Name="seivesTorGrid" ShowGridLines="False" Margin="10, 0, 10, 0"
                  Width="Auto" Height="Auto" >

                <Grid.ColumnDefinitions> </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" MinHeight="25"/>
                </Grid.RowDefinitions>
            </Grid>

This is how I add Cols & Row to the Grid :

        private void AddColumns()
    {
        ColumnDefinition newCol = new ColumnDefinition();
        //newCol.Width = GridLength.Auto;
        newCol.MinWidth = 50;
        newCol.MaxWidth = 150;
        seivesTorGrid.ColumnDefinitions.Add(newCol);

        GridViewColumnHeader gch = new GridViewColumnHeader();
        gch.Content = "ID";
        gch.FontSize = 12;

        Grid.SetColumn(gch, 0);
        seivesTorGrid.Children.Add(gch);

        for (int i = 0; i < GlobalUtils.TOR_List.Count; i++)
        {
            newCol = new ColumnDefinition();
            newCol.Width = GridLength.Auto;
            newCol.MinWidth = 80;
            newCol.MaxWidth = 150;                
            seivesTorGrid.ColumnDefinitions.Add(newCol);

            gch = new GridViewColumnHeader();
            gch.Content = GlobalUtils.TOR_List[i].TOR_Id;
            gch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            gch.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            gch.FontSize = 12;

            Grid.SetColumn(gch, i + 1);
            seivesTorGrid.Children.Add(gch);                
        }

    }

    private void AddRowToGrid(String id)
    {
        RowDefinition newRow = new RowDefinition();

        seivesTorGrid.RowDefinitions.Add(newRow);
        int rowIndex = seivesTorGrid.RowDefinitions.Count - 1;

        TextBlock idTB = new TextBlock();
        idTB.Text = id;
        idTB.TextAlignment = TextAlignment.Left;
        idTB.VerticalAlignment = System.Windows.VerticalAlignment.Center;

        Grid.SetColumn(idTB, 0);
        Grid.SetRow(idTB, rowIndex);
        seivesTorGrid.Children.Add(idTB);

        for (int i = 1; i < 3; i++)
        {
            Button tempBtn = new Button() {
                Height=25, 
                Width=80,
                Margin = new Thickness(5),
                Content = "Add", 
                Background = Brushes.Brown, 
                Tag = rowIndex
            };

            tempBtn.Click += OnIdButtonClick;

            Grid.SetColumn(tempBtn, i);
            Grid.SetRow(tempBtn, rowIndex);                
            seivesTorGrid.Children.Add(tempBtn);
        }                       
    }

Why the result after deleting the row is not proper? What needs to be corrected?

Any help is highly appreciated.

Upvotes: 0

Views: 99

Answers (1)

Clemens
Clemens

Reputation: 128060

You would of course also have to delete all children in that row.

That means all elements from the seivesTorGrid.Children collection, where Grid.GetRow() returns the index that is to be deleted.

Upvotes: 1

Related Questions