Hello!
I have a simple page with just a data grid. The GridLinesVisibility property for the data grid is set to Both, to display both row and column lines. I also set the grid line color black and give it a thickness of 2, so that it's more visible:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:local="clr-namespace:Playground.Maui._7._0.FixingTableGridLines"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sfgrid="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
x:DataType="local:GridContentPageViewModel"
x:Class="Playground.Maui._7._0.FixingTableGridLines.GridContentPage"
Title="GridContentPage">
<VerticalStackLayout>
<sfgrid:SfDataGrid x:Name="TableDataGrid"
IsVisible="True"
VerticalOptions="FillAndExpand"
ColumnWidthMode="Auto"
AllowEditing="False"
NavigationMode="Cell"
SortingMode="Single"
ItemsSource="{Binding RowsCollection}"
QueryRowHeight="TableDataGridQueryRowHeight"
AutoGenerateColumnsMode="None"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both">
<sfgrid:SfDataGrid.DefaultStyle>
<sfgrid:DataGridStyle GridLineStrokeThickness="2"
GridLineColor="Black"/>
</sfgrid:SfDataGrid.DefaultStyle>
</sfgrid:SfDataGrid>
</VerticalStackLayout>
</ContentPage>
In the code-behind for this page, I am creating 3 columns for the data grid in the constructor:
public GridContentPage()
{
InitializeComponent();
// Create the grid columns.
this.TableDataGrid.Columns.Add(new DataGridTemplateColumn()
{
HeaderText = "Column 0",
MaximumWidth = 200,
MappingName = $"GridCellViewModels[0].MappingValue",
CellTemplate = new DataTemplate(() => this.CreateGridCellView(columnIndex: 0))
});
this.TableDataGrid.Columns.Add(new DataGridTemplateColumn()
{
HeaderText = "Column 1",
MaximumWidth = 200,
MappingName = $"GridCellViewModels[1].MappingValue",
CellTemplate = new DataTemplate(() => this.CreateGridCellView(columnIndex: 1))
});
this.TableDataGrid.Columns.Add(new DataGridTemplateColumn()
{
HeaderText = "Column 2",
MaximumWidth = 200,
MappingName = $"GridCellViewModels[2].MappingValue",
CellTemplate = new DataTemplate(() => this.CreateGridCellView(columnIndex: 2))
});
this.BindingContext = new GridContentPageViewModel();
}
The CellTemplate for the columns is created by calling the CreateGridCellValue() method, which sets the binding for the cell value that is displayed:
public GridCellView CreateGridCellView(int columnIndex)
{
GridCellView gridCellView = new GridCellView();
// Set up the bindings.
gridCellView.SetBinding(
GridCellView.CellValueProperty,
$"{nameof(GridRowViewModel.GridCellViewModels)}[{columnIndex}].{nameof(GridCellViewModel.CellValue)}",
BindingMode.OneWay);
return gridCellView;
}
The view model for the Content Page has a RowsCollection property that the grid binds to. This is a collection of GridRowViewModels, and it is filled in the constructor:
public class GridContentPageViewModel
{
public ObservableCollection<GridRowViewModel> RowsCollection { get; } = new ObservableCollection<GridRowViewModel>();
public GridContentPageViewModel()
{
// Add some rows.
RowsCollection.Add(new());
RowsCollection.Add(new());
RowsCollection.Add(new());
}
}
The GridRowViewModel contains a list of cells for each of the three columns:
public class GridRowViewModel
{
// This is the list of the CellViewVMs for each grid cell on this row.
public List<GridCellViewModel> GridCellViewModels { get; } = new List<GridCellViewModel>();
public GridRowViewModel()
{
this.GridCellViewModels.AddRange(new List<GridCellViewModel>()
{
new GridCellViewModel()
{
CellValue = "CellValue 1",
MappingValue = 1
},
new GridCellViewModel()
{
CellValue = "CellValue 2",
MappingValue = 2
},
new GridCellViewModel()
{
CellValue = "CellValue 3",
MappingValue = 3
},
});
}
}
The GridCellViewModel contains the value to be displayed in the cell, and the mapping value for sorting:
public class GridCellViewModel
{
public string CellValue { get; set; }
public int MappingValue { get; set; }
}
However, when I run this, the grid lines are shown for the column headers but are missing from the cells, as shown below. I suspect it has something to do with the fact that I'm displaying a custom ContentView for the grid cell, but I'm not sure.
How do I show grid lines when I create custom columns with a custom ContentView or the grid cells?
Hi Alfredo,
Based on the provided code snippet, we have tested the reported issue of missing grid lines in DataGrid rows or columns. However, we were unable to replicate the issue on our end. We have shared the tested sample and a screenshot for your reference. If you are still encountering the same problem, we kindly request you to modify the attached sample to reproduce the issue and provide additional details. This will greatly assist us in investigating the issue further and providing an appropriate solution as quickly as possible.
Regards,
Sethupathy D.
Hi Sethupathy, thank you for your prompt response.
I was unable to run the solution you sent me, but I was able to open it and view the code. The only difference I see is that in your project, the GridCellView is a ContentView defined only through code, and it only contains a Label:
public class GridCellView : ContentView
{
public static readonly BindableProperty CellValueProperty =
BindableProperty.Create(nameof(CellValue), typeof(string), typeof(GridCellView), string.Empty);
public string CellValue
{
get => (string)GetValue(CellValueProperty);
set => SetValue(CellValueProperty, value);
}
public GridCellView()
{
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding(nameof(CellValue), source: this));
Content = label;
}
}
However, in my code, the GridCellView is a ContentView defined using XAML. It also contains a Label within a StackLayout:
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Playground.Maui._7._0.FixingTableGridLines"
x:DataType="local:GridCellView"
x:Name="this"
x:Class="Playground.Maui._7._0.FixingTableGridLines.GridCellView">
<StackLayout BindingContext="{x:Reference this}"
BackgroundColor="LightYellow">
<Label Text="{Binding CellValue}"
HeightRequest="50"
HorizontalOptions="Center"/>
</StackLayout>
</ContentView>
Here is the code-behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GridCellView : ContentView
{
public static readonly BindableProperty CellValueProperty = BindableProperty.Create(
nameof(CellValue),
typeof(string),
typeof(GridCellView),
null,
BindingMode.OneWay);
public string CellValue
{
get => (string)GetValue(CellValueProperty);
set => SetValue(CellValueProperty, value);
}
public GridCellView()
{
InitializeComponent();
}
}
It also looks like the problem may be caused by the setting of the BackgroundColor property in the StackLayout. When I remove this property, the grid lines are displayed.
Is this a problem in the rendering logic? Is there a way to set the background color of the cell view, and still display the grid lines?
Hi
Alfredo,
We have checked your query at our end, and we are able to replicate that issue
at our end. We have considered this as a bug and logged a bug report
regarding this in our feedback portal. We will fix the reported issue and include
the changes in our upcoming weekly patch release, which is expected to be
rolled out on September 10, 2024. We will let you know once released, and we
appreciate your patience until then. You can also track the status of the bug
using the feedback link below.
Feedback Link: 60560
Disclaimer: Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.
Regards,
Sethupathy D.
Hi Alfredo,
We are glad to inform you that the reported issue regarding the “Grid Lines are not visible when loading template column with background color” has been resolved. This fix has been incorporated into our latest weekly NuGet release. To benefit from this fix, we recommend updating your DataGrid package to version 26.2.11.
https://www.nuget.org/packages/Syncfusion.Maui.DataGrid
Root Cause,
We have drawn the grid lines beneath the content, which causes them to become invisible in the UI when the background color is applied.
We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you require any further assistance.
Regards,
Sethupathy D.
Awesome, you guys are great!
Thanks!
Hi Alfredo,
We hope that you are satisfied with our suggested fix, please let us know if you need further assistance on this. Feel free to open a new forum thread if any new issue that may arise.
Regards,
Jayashree