Click here to Skip to main content
15,179,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a wpf Data-grid that displays datas from an sqlite Database. Each rows of the DataGrid Has a Delete Button

I wrote an event for the Delete button, to delete the The DataGrid Row that the button is in, but it's not working, The row is not deleting.

What I have tried:

                                            <datagridtextcolumn header="Class Name" width="*" minwidth="70" binding="{Binding name}" />
                                            <datagridtextcolumn header="Staff First Name" width="*" minwidth="70" binding="{Binding f_name}" />
                                            <datagridtextcolumn header="Staff Last Name" width="*" minwidth="70" binding="{Binding l_name}" />

                                            <datagridtextcolumn x:name="DataGridTextColumn_class_id" header="Staff class_id" width="auto" minwidth="70" binding="{Binding class_id}" visibility="Collapsed" xmlns:x="#unknown" />
                                            <datagridtemplatecolumn header="Manage" width="auto" minwidth="110">
                                                        <Button x:Name="btn_DeleteListClass" Content="Delete" FontSize="14" HorizontalAlignment="Center" Height="30" Margin="3" Style="{DynamicResource DangerButtonStyle}" Width="100" Click="btn_DeleteListClass_Click" />


private void btn_DeleteListClass_Click(object sender, RoutedEventArgs e)
                //SQLiteConnection con = new SQLiteConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
                SQLiteConnection con = new SQLiteConnection("  Data Source=system.sqlite; Version=3; Compress=True; ");
                SQLiteCommand cmd;

                DataRowView row = (DataRowView)((Button)e.Source).DataContext;

                string query = " DELETE FROM icon_class WHERE class_id = @1 ";
                cmd = new SQLiteCommand(query, con);
                cmd.Parameters.Add(new SQLiteParameter("@1", row));
                if (MessageBox.Show("Do you want to delete this record", "Delete Class", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)

            catch (Exception ex) { MessageBox.Show(ex.Message); }

Updated 8-Mar-16 1:52am

1 solution

You're trying to pass a DataRowView as the value of a parameter. That's not going to work. You need to pass the value of one of the row's columns as the parameter value instead.

For example:
cmd.Parameters.Add(new SQLiteParameter("@1", row["class_id"]));
Member 11197984 8-Mar-16 7:16am
Woow!!!, Thanks Very Much, It Worked. You saved me some time.

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