Category / Section
How to align Xamarin.Android DataGrid in the center of its parent?
1 min read
The SfDataGrid can be aligned to the center of its parent, by setting the gravity of its parent to center and by setting appropriate layout parameters to the grid.
Refer the below code example in which the gravity of the grid’s parent is set as “center” and its layout parameters is set as wrap_content to display the grid in the center of its parent.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/frameLayout" > </FrameLayout>
The below code illustrates setting width and height constraints to a SfDataGrid.
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); viewModel = new ViewModel(); var dataGrid = new SfDataGrid(this); dataGrid.ItemsSource = viewModel.Collection; dataGrid.LayoutParameters.Height = 1000; dataGrid.LayoutParameters.Width = 800; var frameLayout = FindViewById<FrameLayout>(Resource.Id.frameLayout); frameLayout.AddView(dataGrid); }
Screenshot:
Sample Link: