Xamarin.Forms Insert Row to Grid and re-arrange

Viewed 1174

I'm trying to insert new object into existing Grid at row 0 (shifting the remaining rows one lower). Is there a way to do it? Similarly to Log, last item goes in first position. Note that I cannot use ListView, I already have one in the content. Also, I'd prefer to use Grid as I can structure it better for presentation wise etc. Finished grid structure:

> <Grid.RowDefinitions>
>     <RowDefinition Height="*"/> </Grid.RowDefinitions>

> <Grid.ColumnDefinitions>
>      <ColumnDefinition/>
>      <ColumnDefinition/>
>      <ColumnDefinition/>  
</Grid.ColumnDefinitions>
> (existing Labels)
> <Label Text="1" Grid.Column="0" Grid.Row="0"/> 
<Label Text="2" Grid.Column="0" Grid.Row="0"/> 
> <Label Text="3", Grid.Column="0", Grid.Row="0"/>
>  </>

I'm generating the grid programmatically, to fill out the above structure (iterate column / rows nr), then attempt to insert top row with Child:

MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row

               

>  MyGrid.Children.Add(new Label
>                         {
>               Text = "original row",
>                             TextColor = Color.Black,
>                             LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
>                             HorizontalTextAlignment = TextAlignment.End,
>                             FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
>                         }, 0, 0); //Column / Row

...

> MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row
> at 0 row index
> 
> 
>                         MyGrid.Children.Add(new Label
>                         {
>               Text = "new row",
>                             TextColor = Color.Black,
>                             LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
>                             HorizontalTextAlignment = TextAlignment.End,
>                             FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
>                         }, 0, 0); //Column / Row

"new row" will overlap "original row"

EDIT: So far, that's what I've done. This is only for one row shift,no column shift.

I could not get Grid Child column/row by

var left = Grid.Children[0].Left();//Experimental flag So I will have to iterate more.

... add new Rows with Labels, 0 column (By default, Grid has 1 Column, 1 Grid), then:

Grid.RowDefinitions.Add(newRow); 

for (int i = Grid.Children.Count -1 ; i >= 0; i--) 
{ 
     var child = > Grid.Children[i]; 
     Grid.Children.RemoveAt(i); 
     Grid.Children.Add(child, 0, i +1); 
} 
Grid.Children.Add(SomeLabel, 0, 0);
2 Answers

To shift all the rows in a grid by one, Grid.GetRow() and Grid.SetRow() methods can be used like the sample code below.

foreach (var child in grid.Children)
{
    var row = Grid.GetRow(child);
    Grid.SetRow(child, row + 1);
}

You can put you Label to List, then achieve it.

For example, If I want add a label in the first row and second Column place, here is running GIF.

enter image description here

Here is my Grid layout.

<StackLayout Margin="10">
      
        <Button Text="add" Clicked="Button_Clicked"></Button>
        <Grid x:Name="MyGrid">
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </Grid>
</StackLayout>

Here is my layout background code. I add three Labels in List, then add the items in list to the Grid. when click the Button, add a new Label to the row 0, column 1, the position in listview is 1.

public partial class MainPage : ContentPage
{
    List<Label> labels;

    public MainPage()
    {
        InitializeComponent();
        labels = new List<Label>();
        labels.Add(new Label
        {
            Text = "1",
          
            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });
        labels.Add(new Label
        {
            Text = "2",

            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });
        labels.Add(new Label
        {
            Text = "3",

            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });

        for (int i=0; i<labels.Count ;i++)
        {
            //If we set the three columns, we can set the Column and  Row by i%3 i/3
            MyGrid.Children.Add(labels[i],i%3,i/3);
        }
    }
    
    private void Button_Clicked(object sender, EventArgs e)
    {
        // Remove the data in the original Gird
        for (int i = 0; i < labels.Count; i++)
        {
            if (MyGrid.Children.Count>0)
            {
                var label = labels[i];

                if (label != null)
                {
                    MyGrid.Children.Remove(label) ;
                }
            }
        }

        // add label
        var newLabel = new Label
        {
            Text = "new row",
            TextColor = Color.Black,
            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        };

        // If I want add a label in the first row and second Column place, I can add the new label to with `Insert` method in List.
        labels.Insert(1,newLabel);

        // re-add the list data to grid.
        for (int i = 0; i < labels.Count; i++)
        {
            MyGrid.Children.Add(labels[i], i % 3, i / 3);
        }
    }
}
Related