1
votes

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>
1
I think you may find you answer here [stackoverflow.com/questions/6882306/…sohaiby

1 Answers

2
votes

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>