Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have one question regarding my WPF project.

I created a DataGrid which I linked to a DataBase (Table). My question is how can I delete a selected row (using btn_click) from the DataGrid and also delete the same data from the DataBase (Tabele).

Thanks in advance!
Updated 3-Aug-19 22:13pm
Sergey Alexandrovich Kryukov 5-Sep-15 21:35pm    
Use data binding and remove bound record from data.

Share this answer
Sergey Alexandrovich Kryukov 6-Sep-15 9:24am    
5ed. Looks instructive.
Thanks Sergey. :)
Hi everyone

I have a datagrid, which I linked to a list as a source, and in a column I put a button to delete the row until the user clicked on that row to delete it.

But I get the following error !!

Thanks for the guidance everyone

xaml code is :

<pre>   <DataGrid VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" IsEnabled="{Binding ElementName=chkitems, Path=IsChecked}"  Height="266" x:Name="dgvitems" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="False" Width="451">
                            <DataGridTextColumn Header="نام کالا" Width="250" FontSize="14" Binding="{Binding name}"/>
                            <DataGridTextColumn Header="تعداد" Width="80" FontSize="14" Binding="{Binding number}"/>
                            <DataGridTemplateColumn Header=" ">
                                <DataGridTemplateColumn.CellTemplate >
                                        <Button x:Name="btndelete"  VerticalAlignment="Center" HorizontalAlignment="Center"  BorderBrush="{x:Null}" Click="Btndelete_Click" ToolTip="حذف رکورد" Background="{x:Null}">
                                            <materialDesign:PackIcon Kind="CloseCircle" Foreground="Red" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>

and mi list is :

<pre lang="c#">

 public class datagridset
            public string name { get; set; }
            public int number { get; set; }


             <pre> private void Window_Loaded(object sender, RoutedEventArgs e)
            List<datagridset> datagridsets = new List<datagridset>();
            dgvitems.ItemsSource = datagridsets.ToList();

                 private void Btndelete_Click(object sender, RoutedEventArgs e)
Share this answer
Richard Deeming 8-Aug-19 13:54pm    
If you want to ask a question, then ASK A QUESTION[^].

Your question is not a solution to someone else's question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900