ListBox not updating when listBoxItem is deleted in C #

How to update UserInterface when list item is removed from list. The ListBoxItem is removed from the database, but the list is not updated. When I click on the pad and navigate to the page, it is removed. Someone tell me how to update the list. I think it has something to do with the observable collection, I don't know how to implement the NotifyProperty for this code. Can anyone help me?

C # code to insert, read and delete items from the database

 public class DatabaseHelperClass
{

    SQLiteConnection dbConn;

    //Create Tabble 
    public async Task<bool> onCreate(string DB_PATH)
    {
        try
        {
            if (!CheckFileExists(DB_PATH).Result)
            {
                using (dbConn = new SQLiteConnection(DB_PATH))
                {
                    dbConn.CreateTable<Data>();
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    private async Task<bool> CheckFileExists(string fileName)
    {
        try
        {
            var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
            return true;
        }
        catch (NullReferenceException exp)
        {
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    // Retrieve the specific contact from the database. 
    public Data ReadContact(int contactid)
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            var existingconact = dbConn.Query<Data>("select * from Contacts where Id =" + contactid).FirstOrDefault();
            return existingconact;
        }
    }
    // Retrieve the all contact list from the database. 
    public ObservableCollection<Data> ReadContacts()
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            List<Data> myCollection = dbConn.Table<Data>().ToList<Data>();
            ObservableCollection<Data> ContactsList = new ObservableCollection<Data>(myCollection);

            return ContactsList;
        }

}


    // Insert the new contact in the Contacts table. 
    public void Insert(Data newcontact)
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            dbConn.RunInTransaction(() =>
            {
                dbConn.Insert(newcontact);
            });
        }
    }

    //Delete specific contact 
    public void DeleteContact(int Id)
    {
         using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            var existingconact = dbConn.Query<Data>("select * from Data where Id =" + Id).FirstOrDefault();
            if (existingconact != null)
            {
                dbConn.RunInTransaction(() =>
                {
                    dbConn.Delete(existingconact);
                });
            }
        }
    }
    //Delete all contactlist or delete Contacts table 
    public void DeleteAllContact()
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            //dbConn.RunInTransaction(() => 
            //   { 
            dbConn.DropTable<Data>();
            dbConn.CreateTable<Data>();
            dbConn.Dispose();
            dbConn.Close();
            //}); 
        }
    }
}

      

Code to remove an item from the list.

 public partial class Panaroma : Page
{
    ObservableCollection<Data> DB_ContactList = new ObservableCollection<Data>();


    public Panaroma()
    {
        this.InitializeComponent();
        if(listBoxobj.Items.Count == 0)
        {
            Btn_Delete.IsEnabled = false;
        }


        this.Loaded += Panaroma_Loaded;

    }

    private void Panaroma_Loaded(object sender, RoutedEventArgs e)
    {
        ReadAllData dbcontacts = new ReadAllData();

        DB_ContactList = dbcontacts.GetAllContacts();//Get all DB contacts 
        if (DB_ContactList.Count > 0)
        {
            Btn_Delete.IsEnabled = true;
        }
        listBoxobj.ItemsSource = DB_ContactList.OrderByDescending(i => i.Id).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first.

    }


    private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int SelectedContactID = 0;
        if (listBoxobj.SelectedIndex != -1)
        {
            Data listitem = listBoxobj.SelectedItem as Data;//Get slected listbox item 
            DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
            Db_Helper.DeleteContact(listitem.Id);//Delete selected DB contact Id.
        }
    }

      

+3


source to share


1 answer


To remove an item from the collection, use the Remove

(listitem) msdn method and update the associated ListBox.

private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int SelectedContactID = 0;
    if (listBoxobj.SelectedIndex != -1)
    {
        Data listitem = listBoxobj.SelectedItem as Data;//Get slected listbox item 
        DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
        Db_Helper.DeleteContact(listitem.Id);//Delete selected DB contact Id.
        //remove item from collection
        DB_ContactList.Remove(listitem); 
        //update listbox
        //...
    }
}

      



To update the ListBox, see the post

+2


source







All Articles