Beispiel #1
0
        private void LijstCategorie_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:

                foreach (Categorie cat in e.NewItems)
                {
                    if (!Startup)
                    {
                        try
                        {
                            lblStatus.Content = "Categorie opslaan";
                            connection.Open();
                            var command =
                                new SqlCommand(
                                    "insert into categorie (titel) values ('" + cat.Titel +
                                    "'); select SCOPE_IDENTITY();",
                                    connection);

                            var result = (decimal)command.ExecuteScalar();

                            cat.Id = Convert.ToInt32(result);

                            var categorieItem = new TreeViewItem
                            {
                                Header = cat.Titel,
                                Tag    = cat
                            };

                            lblStatus.Content = "Categorie opgeslagen";

                            CategorieLijst.Items.Add(categorieItem);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            connection.Close();
                            cat.NotitieLijst.CollectionChanged += categorie_NotitieLijst_CollectionChanged;
                        }
                    }
                    else
                    {
                        var categorieItem = new TreeViewItem
                        {
                            Header = cat.Titel,
                            Tag    = cat
                        };

                        CategorieLijst.Items.Add(categorieItem);
                        cat.NotitieLijst.CollectionChanged += categorie_NotitieLijst_CollectionChanged;
                    }
                }

                break;

            case NotifyCollectionChangedAction.Remove:

                foreach (Categorie cat in e.OldItems)
                {
                    try
                    {
                        lblStatus.Content = "Categorie verwijderen";
                        connection.Open();
                        var commandnote = new SqlCommand("delete from notitie where cat_id = " + cat.Id, connection);
                        commandnote.ExecuteNonQuery();
                        var command = new SqlCommand("delete from categorie where id = " + cat.Id, connection);
                        command.ExecuteNonQuery();
                        connection.Close();

                        //UI stuff
                        CategorieLijst.Items.Remove(CategorieLijst.SelectedItem);
                        notetext.Clear();
                        NotitieTextBox.Clear();
                        NotitieTextBox.IsEnabled = false;
                        lblStatus.Content        = "Categorie verwijdert";
                    }
                    catch (Exception ex)
                    {
                        connection.Close();
                        MessageBox.Show(ex.Message);
                    }
                }

                break;
            }
        }
Beispiel #2
0
        private void categorie_NotitieLijst_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                foreach (Notitie.Notitie noteItem in e.NewItems)
                {
                    if (Startup)
                    {
                        continue;
                    }
                    try
                    {
                        lblStatus.Content = "Notitie opslaan...";
                        connection.Open();
                        TreeViewItem selectedcategory = (TreeViewItem)CategorieLijst.SelectedItem;
                        var          categorie        = (Categorie)selectedcategory.Tag;
                        var          command          =
                            new SqlCommand(
                                "insert into notitie (titel, tekst, cat_id) values ('" + noteItem.Titel + "', '" +
                                noteItem.Text + "', " + categorie.Id + "); select SCOPE_IDENTITY();",
                                connection);
                        var result = (decimal)command.ExecuteScalar();
                        noteItem.id = Convert.ToInt32(result);
                        var treeItem = new TreeViewItem
                        {
                            Header = noteItem.Titel,
                            Tag    = noteItem
                        };
                        NotesLijst.Items.Add(treeItem);
                        lblStatus.Content = "Notitie opgeslagen";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }


                break;

            case NotifyCollectionChangedAction.Remove:

                foreach (Notitie.Notitie note in e.OldItems)
                {
                    NotesLijst.Items.Remove(note);

                    try
                    {
                        lblStatus.Content = "Notitie verwijderen...";
                        connection.Open();
                        var command = new SqlCommand("delete from notitie where id = " + note.id, connection);
                        command.ExecuteNonQuery();
                        NotesLijst.Items.Remove(NotesLijst.SelectedItem);

                        //UI stuff
                        NotitieTextBox.Clear();
                        NotitieTextBox.IsEnabled = false;
                        notetext.Clear();
                        lblStatus.Content = "Notitie verwijdert";
                    }
                    catch (Exception w)
                    {
                        MessageBox.Show(w.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }

                break;
            }
        }