Click here to Skip to main content
15,896,557 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!
Posted
Updated 3-Aug-19 22:13pm
Comments
Sergey Alexandrovich Kryukov 5-Sep-15 21:35pm    
Use data binding and remove bound record from data.
—SA

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Sep-15 9:24am    
5ed. Looks instructive.
—SA
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 :

XML
<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">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="نام کالا" Width="250" FontSize="14" Binding="{Binding name}"/>
                            <DataGridTextColumn Header="تعداد" Width="80" FontSize="14" Binding="{Binding number}"/>
                            <DataGridTemplateColumn Header=" ">
                                <DataGridTemplateColumn.CellTemplate >
                                    <DataTemplate>
                                        
                                        <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"/>
                                        </Button>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                        
                    </DataGrid>






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)
        {
           if(dgvitems.SelectedItem!=null)
            {
                dgvitems.Items.Remove(dgvitems.SelectedItem);
            }
        }
 
Share this answer
 
Comments
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