Articles in this section
Category / Section

How to set right margin in the Header in SfDataGrid?

1 min read

 

 

In SfDataGrid, when you set TextMargin in GridColumn, it will apply the margin only for the record cells and not the header cells. However, you want to set the TextMargin in header, to override the GridHeaderCellRenderer, and append some spaces to the text of UI Label following a period and make the color of the period appended at the end of the transparent, such that it will be look like text is set with some margin at the right end. Otherwise we include transparent period at the end of the string after the spaces and set the TextMargin based on the record cells.

In following code sample ensure that the text is set with the margin at right end.               

public class CustomGridHeaderCellRenderer : GridHeaderCellRenderer
{
    public override void OnInitializeDisplayView(DataColumnBase dataColumn, UIView view)
    {
           base.OnInitializeDisplayView(dataColumn, view);
 
           UILabel label = view as UILabel;
 
          if (label == null)
              return;
 
          label.LineBreakMode = UILineBreakMode.WordWrap;
          label.Lines = 2;
          if (dataColumn.GridColumn.HeaderTextAlignment == UITextAlignment.Right)
          {
              label.Text += "  .";
              var textAttributed = new NSMutableAttributedString(label.Text);
              var colorAttribute = new UIStringAttributes()
              {
                  ForegroundColor = UIColor.Clear
              };
              textAttributed.SetAttributes(colorAttribute, new NSRange(label.Text.Length - 1, 1));
              label.AttributedText = textAttributed;
        }
    }
}

 

Output:

Refer the following screenshot that shows the final outcome on execution of the above code.

 

 

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

Right margin in header

 

               

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