Skip to content

Commit

Permalink
allow naming new columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 27, 2023
1 parent f32a47a commit cf445ae
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Maui.DataGrid.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ public MainPage()
BindingContext = new MainViewModel();
}

private void OnAddColumn(object sender, EventArgs e)
private async void OnAddColumn(object sender, EventArgs e)
{
_dataGrid1.Columns.Add(new DataGridColumn() { Title = "Test", Width = new(100) });
var newColumnTitle = await Shell.Current.DisplayPromptAsync("Add column", "What is the name of the new column?");

if (string.IsNullOrEmpty(newColumnTitle))
{
await Shell.Current.DisplayAlert("Title required", "A title is required in order to add a column.", "Ok");
}
else
{
_dataGrid1.Columns.Add(new DataGridColumn() { Title = newColumnTitle, Width = new(100) });
}
}

private async void OnRemoveColumn(object sender, EventArgs e)
{
var columnTitle = await Shell.Current.DisplayPromptAsync("Which column should be removed?", "Remove column");
var columnTitle = await Shell.Current.DisplayPromptAsync("Remove column", "Which column should be removed?");

var teamColumn = _dataGrid1.Columns.FirstOrDefault(c => c.Title == columnTitle);
var columnToRemove = _dataGrid1.Columns.FirstOrDefault(c => c.Title == columnTitle);

if (teamColumn != null)
if (columnToRemove == null)
{
_ = _dataGrid1.Columns.Remove(teamColumn);
await Shell.Current.DisplayAlert("Column not found", "No column by that title", "Ok");
}
else
{
await Shell.Current.DisplayAlert("Column not found", "No column by that title", "Ok");
_ = _dataGrid1.Columns.Remove(columnToRemove);
}
}
}

0 comments on commit cf445ae

Please sign in to comment.