Articles in this section
Category / Section

How to display RowIndex at RowHeaderCell in Xamarin.Android SfDataGrid?

4 mins read

RowHeader can be enabled by setting “SfDataGrid.ShowRowHeader” as true. To display row index in RowHeaderCell you need to customize the GridRowHeaderCellRenderer and replace it in the “SfDataGrid.CellRenderers” collection.

Refer the below code example to create a new customized renderer for RowHeader and replace it in the cell renderers collection of SfDataGrid.

public class MainActivity : Activity
{
    SfDataGrid sfGrid;
    ViewModel viewModel;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        sfGrid = new SfDataGrid(this);
        viewModel = new ViewModel();
        sfGrid.ItemsSource = viewModel.ProductDetails;
        sfGrid.AutoGenerateColumns = true;
        sfGrid.ShowRowHeader = true;
        sfGrid.RowHeaderWidth = 130;
 
        sfGrid.CellRenderers.Remove("RowHeader");
        sfGrid.CellRenderers.Add("RowHeader", new CustomGridRowHeaderCellRenderer());
        SetContentView(sfGrid);
    }
}

 

The below code illustrates how to customize the GridRowHeaderCellRenderer to display row index in RowHeaderCell in SfDataGrid.

public class CustomGridRowHeaderCellRenderer : GridRowHeaderCellRenderer
{
    TextView gridRowHeaderText;
    GridRowHeaderCell gridRowHeaderCell;
    public CustomGridRowHeaderCellRenderer()
    {
 
    }
 
    protected override GridRowHeaderCell OnCreateDisplayUIView()
    {
        gridRowHeaderText = new TextView(this.Context);
        gridRowHeaderText.SetTextColor(Android.Graphics.Color.Black);
        gridRowHeaderText.Gravity = GravityFlags.Center;
        gridRowHeaderText.TextAlignment = TextAlignment.Center;
        gridRowHeaderText.Layout(this.DataGrid.Left, this.DataGrid.Top, this.DataGrid.Right, this.DataGrid.Bottom);
        gridRowHeaderCell = new GridRowHeaderCell(this.Context);
        gridRowHeaderCell.AddView(gridRowHeaderText);
        return gridRowHeaderCell;
    }
 
    public override void OnInitializeDisplayView(DataColumnBase dataColumn, GridRowHeaderCell view)
    {
        gridRowHeaderText = view.GetChildAt(0) as TextView;
        if (dataColumn.RowIndex == 0)
        {
            gridRowHeaderText.Text = "RowHeader";
            gridRowHeaderText.SetTypeface(null, TypefaceStyle.Bold);
        }
        else
            gridRowHeaderText.Text = dataColumn.RowIndex.ToString();
        base.OnInitializeDisplayView(dataColumn, view);
    }
 
    protected override void OnLayout(RowColumnIndex rowColumnIndex, View view, int left, int top, int right, int bottom)
    {
        base.OnLayout(rowColumnIndex, view, left, top, right, bottom);
        gridRowHeaderText = (view as GridRowHeaderCell).GetChildAt(0) as TextView;
        gridRowHeaderText.Layout(left, top, right, bottom);
    }
 
    public override void OnUpdateDisplayValue(DataColumnBase dataColumn, GridRowHeaderCell view)
    {
        gridRowHeaderText.Text = dataColumn.RowIndex.ToString();
        base.OnUpdateDisplayValue(dataColumn, view);
    }
}

 

Screenshot

C:\Users\indhumathy.malayappa\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Captureaa.png

Sample

 Display the RowIndex in RowHeaderCell

 

Conclusion

I hope you enjoyed learning about how to display RowIndex at RowHeaderCell in SfDataGrid in Xamarin.

You can refer to our Xamarin 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 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