Add delete button for every row in datagrid

.net, datagrid, wpf

Solution

You can add a `DataGridTemplateColumn` and add a `Button` to its `CellTemplate`. Then use the built in `ApplicationCommands.Delete` or your own `ICommand` for the `Button`

<DataGrid ItemsSource="{Binding Results}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="FriendlyName"
                            Binding="{Binding FriendlyName}"/>
        <DataGridTextColumn Header="Id"
                            Binding="{Binding Id}"/>
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Delete"
                            Command="Delete"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Update If you don't want to use the built in `Delete` you can use your own `ICommand`. Here is a short example with `RelayCommand` which can be found in the following link: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx. Also, I uploaded it here: RelayCommand.cs

<DataGrid ItemsSource="{Binding Results}"
          SelectedItem="{Binding SelectedResult}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- ... -->
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Delete"
                            Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                                              Path=DataContext.DeleteCommand}"
                            CommandParameter="{Binding}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

And in your viewmodel or code behind

private Result m_selectedResult;
public Result SelectedResult
{
    get { return m_selectedResult;}
    set
    {
        m_selectedResult = value;                
        OnPropertyChanged("SelectedResult");
    }
}

private bool CanDelete
{
    get { return SelectedResult != null; }
}

private ICommand m_deleteCommand;
public ICommand DeleteCommand
{
    get
    {
        if (m_deleteCommand == null)
        {
            m_deleteCommand = new RelayCommand(param => Delete((Result)param), param => CanDelete);
        }
        return m_deleteCommand;
    }
}

private void Delete(Result result)
{
    Results.Remove(result);
}

Problem

how to add a delete button for every row in a data grid (contains two fields) in WPF. while datagrid itemsource is ``` ObservableCollection<Result> ``` and ``` public class Result : INotifyPropertyChanged { public string FriendlyName { get; set; } public string Id { get; set; } public Result(string name, string id) { this.FriendlyName = name; this.Id = id; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string property) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(property)); } } #endregion } ``` }

Original source