Articles in this section
Category / Section

How to show multi-line header text in Xamarin.iOS DataGrid?

3 mins read

Xamarin.iOS DataGrid allows you to customize the header text for a GridColumn by using the HeaderText property. However by default the grid displays only single line of text in the header and hence when you load a longer header text, the SfDataGrid clips them and displays an ellipsoid at the end.

 

In case, if longer text need to be displayed in the header, then SfDataGrid allows you to achieve it by creating a custom HeaderCellRenderer derived from HeaderCellRenderer. The custom HeaderCellRenderer replaces the default header view. Hence you can use it to load a label with LineBreakMode property being set to WordWrap. You can also decide the number of lines the text should display by setting the Lines property of the UILabel. You can use the HeaderRowHeight property to customize the header row height as required based on your header text.

 

Refer the following code example which illustrates how to show multiline header in SfDataGrid.

 

CustomHeaderCellRenderer.cs

Public class CustomHeaderCellRenderer: GridHeaderCellRenderer
{
   public CustomHeaderCellRenderer ()
   {
   }
   public override void OnInitializeDisplayView (DataColumnBase dataColumn, UILabel view)
   {
      if (view != null) 
      {
         view.Text = dataColumn.CellValue.ToString ();
         view.Lines = 5;
         view.LineBreakMode = UILineBreakMode.WordWrap;
         view.TextAlignment = UITextAlignment.Center;
     }
   }
}

 

Make sure you add the Custom Renderer to the SfDataGrid’s CellRenderers collection in your ViewController. Refer the following code example.

ViewController.cs

public partial class ViewController : UIViewController
{
   SfDataGrid grid= new SfDataGrid();
   ViewModel viewmodel=new ViewModel();
   public ViewController()
   {
   }
   public override void ViewDidLoad ()
   {
      base.ViewDidLoad ();
      this.View.BackgroundColor = UIColor.White;
      grid.ItemsSource = viewmodel.Info;
      grid.BackgroundColor = UIColor.White;
      grid.HeaderRowHeight = 150;
       //Removing default HeaderCellRenderer and adding custom renderer
      grid.CellRenderers.Remove ("HeaderView");
      grid.CellRenderers.Add ("HeaderView",new CustomHeaderCellRenderer());
      grid.ColumnSizer = ColumnSizer.Star;
      GridTextColumn test = new GridTextColumn ();
      test.MappingName = "ID";
      test.HeaderText = "This is a demo to set multi line text as a column header";
      grid.Columns.Add (test);
      View.AddSubview (grid);
   }
   public override void ViewDidLayoutSubviews ()
   {
      this.grid.Frame = new CGRect (0, 20, View.Frame.Width, View.Frame.Height-20);
      base.ViewDidLayoutSubviews ();
   }   
}

 

The following screenshot shows the final outcome on executing the above code.

Sample image KB

    

You can download the working sample for this KB from the below link.

https://www.syncfusion.com/downloads/support/directtrac/general/ze/MultiLine1032256719


 

Conclusion

I hope you enjoyed learning about how to show multi-line header text in Xamarin.iOS DataGrid


You can refer to our Xamarin.iOS DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Xamarin.iOS DataGrid  example
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