Reputation: 757
How to add a delete column in datagrid in WPF, when i add this column, got Items collection must be empty before using ItemsSource Error
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dgStatus"
HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top"
RenderTransformOrigin="-23.633,-5.198" Height="364" Width="811"
CellEditEnding="myGrid_CellEditEnding" >
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
Upvotes: 1
Views: 1873
Reputation: 7409
You missed <DataGrid.Columns>
tag
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dgStatus"
HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top"
RenderTransformOrigin="-23.633,-5.198" Height="364" Width="811"
CellEditEnding="myGrid_CellEditEnding" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Upvotes: 2