Articles in this section
Category / Section

How to load multiple Labels and SfDataGrids in Xamarin.iOS application?

2 mins read

By customizing the height constraint for the subviews using the HeightAnchor property, you can load multiple UILabel and SfDataGrid in UIStackView.

Refer the below code example in which the HeightAnchor of the UILabel and SfDataGrid is set to load multiple instance in a UIStackView.

public class GridInStackView : UIViewController
{
        private UIStackView stackView;
        private SfDataGrid grid1;
        private UILabel firstLabel;
        private UILabel secondLabel;
        private SfDataGrid grid2;
        private UILabel thirdLabel;
        private SfDataGrid grid3;
        public GridInStackView()
        {
            stackView = new UIStackView();
            stackView.Axis = UILayoutConstraintAxis.Vertical;
                     
            firstLabel = new UILabel();
            firstLabel = GetHeaderLabel("Label1");
 
            grid1 = new SfDataGrid();
            grid1 = GetGrid();
 
            secondLabel = new UILabel();
            secondLabel = GetHeaderLabel("Label2");
 
            grid2 = new SfDataGrid();
            grid2 = GetGrid();
 
            thirdLabel = new UILabel();
            thirdLabel = GetHeaderLabel("Label3");
 
            grid3= new SfDataGrid();
            grid3= GetGrid();
 
            stackView.AddArrangedSubview(firstLabel);
            stackView.AddArrangedSubview(grid1);
            stackView.AddArrangedSubview(secondLabel);
            stackView.AddArrangedSubview(grid2);
            stackView.AddArrangedSubview(thirdLabel);
            stackView.AddArrangedSubview(grid3);
 
            View.BackgroundColor = UIColor.White;
            View.AddSubview(stackView);
 
        }
 
        private SfDataGrid GetGrid()
        {
            SfDataGrid grid = new SfDataGrid();
            grid.ItemsSource = new ViewModel().OrdersInfo;
            grid.SelectionMode = SelectionMode.None;
            grid.ShowRowHeader = false;
            grid.HeightAnchor.ConstraintEqualTo(200).Active = true;
            return grid;
        }
 
        private UILabel GetHeaderLabel(string text)
        {
            var label = new UILabel();
            label.Text = text;
            label.TextAlignment = UITextAlignment.Center;
            label.HeightAnchor.ConstraintEqualTo(20).Active = true;
            label.BackgroundColor = UIColor.Gray;
            return label;
        }
 
        public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();
            stackView.Frame = new CoreGraphics.CGRect(0, 20, this.View.Frame.Width, this.View.Frame.Height);
           
         }
}

 

On executing the above code example, the below output is obtained.

Output in Xamarin

 

Sample Link

How to load multiple UILabels and SfDataGrids in UIStackView?

 

Conclusion

I hope you enjoyed learning about how to load multiple Labels and SfDataGrids in Xamarin.iOS application.

You can refer to our Xamarin.iOS DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.iOS DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied