Click here to Skip to main content
15,891,513 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:

HTML
<datagrid.columns>
                                            <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">
                                                <datagridtemplatecolumn.celltemplate>
                                                    <datatemplate>
                                                        <Button x:Name="btn_DeleteListClass" Content="Delete" FontSize="14" HorizontalAlignment="Center" Height="30" Margin="3" Style="{DynamicResource DangerButtonStyle}" Width="100" Click="btn_DeleteListClass_Click" />
                                                    </datatemplate>
                                                </datagridtemplatecolumn.celltemplate>
                                            </datagridtemplatecolumn>

                                        </datagrid.columns>

private void btn_DeleteListClass_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //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));
                con.Open();
                if (MessageBox.Show("Do you want to delete this record", "Delete Class", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    cmd.ExecuteNonQuery();
                    Load_DataGrid_ListClass();
                    con.Close();
                }

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

        }
Posted
Updated 8-Mar-16 0:52am
v3

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:
C#
cmd.Parameters.Add(new SQLiteParameter("@1", row["class_id"]));
 
Share this answer
 
Comments
Member 11197984 8-Mar-16 7:16am    
Woow!!!, Thanks Very Much, It Worked. You saved me some time.
This only addresses code behind code. There is no solution for event handler or command handler that lives in a databound ViewModel.
No solution for that. It forces code behind and not View Model implementation, or at least a partial mvvm solution.
 
Share this answer
 

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