How can we add columns in xamarin forms grid on the basis of data using MVVM

Ajay87Ajay87 INMember ✭✭

Can we add columns dynamically in xamarin forms grid on the basis of data

Best Answers

  • ColeXColeX Xamurai
    Accepted Answer

    Give x:name on grid and add columns dynamically code behind , the Width of ColumnDefinition should be set as GridUnitType.Star ,it interpret the Value property value as a proportional weight.

            grid.RowDefinitions = new RowDefinitionCollection();
            grid.ColumnDefinitions = new ColumnDefinitionCollection();
    
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });  // only one row
    
            for (int i = 0; i < data.count; i++)
            {            
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            }
    

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Ajay87 said:
    Can we add columns dynamically in xamarin forms grid on the basis of data

    Yes. Use ColumnDefinitions.Add

    e.g. topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

  • Ajay87Ajay87 INMember ✭✭

    @JohnHardman Hi John thanks for reply, i am using MVVM pattern can you please share any sample code for this, it will really helpful to me.

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    Give x:name on grid and add columns dynamically code behind , the Width of ColumnDefinition should be set as GridUnitType.Star ,it interpret the Value property value as a proportional weight.

            grid.RowDefinitions = new RowDefinitionCollection();
            grid.ColumnDefinitions = new ColumnDefinitionCollection();
    
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });  // only one row
    
            for (int i = 0; i < data.count; i++)
            {            
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            }
    
Sign In or Register to comment.