Skip to content

Commit

Permalink
Merge pull request #145 from symbiogenesis/static-columns
Browse files Browse the repository at this point in the history
Don't clear columns every time
  • Loading branch information
symbiogenesis authored Dec 29, 2023
2 parents 01cf11c + dd3e5bc commit 035ee90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 16 additions & 2 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,6 @@ private void InitHeaderView()
SetColumnsBindingContext();

_headerView.Children.Clear();
_headerView.ColumnDefinitions.Clear();
ResetSortingOrders();

_headerView.Padding = new(BorderThickness.Left, BorderThickness.Top, BorderThickness.Right, 0);
Expand All @@ -1274,7 +1273,14 @@ private void InitHeaderView()

col.ColumnDefinition ??= new(col.Width);

_headerView.ColumnDefinitions.Add(col.ColumnDefinition);
if (i > _headerView.ColumnDefinitions.Count - 1)
{
_headerView.ColumnDefinitions.Add(col.ColumnDefinition);
}
else if (_headerView.ColumnDefinitions[i] != col.ColumnDefinition)
{
_headerView.ColumnDefinitions[i] = col.ColumnDefinition;
}

if (!col.IsVisible)
{
Expand All @@ -1288,6 +1294,14 @@ private void InitHeaderView()
Grid.SetColumn(col.HeaderView, i);
_headerView.Children.Add(col.HeaderView);
}

if (_headerView.ColumnDefinitions.Count > Columns.Count)
{
for (var i = _headerView.ColumnDefinitions.Count - 1; i > Columns.Count - 1; i--)
{
_headerView.ColumnDefinitions.RemoveAt(i);
}
}
}

private void ResetSortingOrders()
Expand Down
15 changes: 13 additions & 2 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public object RowToEdit

private void CreateView()
{
ColumnDefinitions.Clear();
Children.Clear();

SetStyling();
Expand All @@ -80,7 +79,14 @@ private void CreateView()
{
var col = DataGrid.Columns[i];

ColumnDefinitions.Add(col.ColumnDefinition);
if (i > ColumnDefinitions.Count - 1)
{
ColumnDefinitions.Add(col.ColumnDefinition);
}
else if (ColumnDefinitions[i] != col.ColumnDefinition)
{
ColumnDefinitions[i] = col.ColumnDefinition;
}

if (!col.IsVisible)
{
Expand All @@ -92,6 +98,11 @@ private void CreateView()
SetColumn((BindableObject)cell, i);
Children.Add(cell);
}

for (var i = ColumnDefinitions.Count - 1; i > DataGrid.Columns.Count - 1; i--)
{
ColumnDefinitions.RemoveAt(i);
}
}

private void SetStyling()
Expand Down

0 comments on commit 035ee90

Please sign in to comment.