title: How to delete items from a collection view in xamarin.forms?
body: <p><strong>NOTE:</strong> this didn't work : <a href="https://stackoverflow.com/questions/20365265/sqlite-error-cannot-delete-wherelistiterator1-it-has-no-pk">SQLite Error: Cannot delete WhereListIterator`1: it has no PK</a>
  (Adding a table name made no change)</p>
  <p>I have an AcceuilPage which basically do this : <a href="https://github.com/devcrux/Xamarin.Forms-Expander" rel="nofollow noreferrer">https://github.com/devcrux/Xamarin.Forms-Expander</a> but i added a delete icon and when i press it i want to be able to delete all the content for this date ( also the date) from this page.</p>
  <p>So i added a delete function to delete it from database but when i expand for a date and press delete i get this error : Exception:
  System.NotSupportedException: Cannot delete Object: it has no PK which bring me to await App.Database.DeleteAgendaAsync(agenda); in  :</p>
  <pre><code>async void DeleteButtonClicked(object sender, EventArgs e)
  {
      
      var agenda = (Agenda)AgendaCollection.SelectedItem;
  
      await App.Database.DeleteAgendaAsync(agenda);
      await Navigation.PopAsync();
  
  }
  </code></pre>
  <p>i also tried for the error line this : await App.Database.DeleteAgendaAsync(agenda); but this didn't even do anything when i pressed it ( no error and nothing happening).</p>
  <p>I saw that this was a working fine for a listview but im struggling to do it in a collectionview, feels to me like it doesn't know if i am currently selecting something. ( With listview, when u click an item, the item view change color).</p>
  <p>What am i doing wrong, im also starting to think that using a listview would be more simple.</p>
  <p><strong>My AcceuilPage.xaml</strong></p>
  <pre><code>&lt;ContentPage.Content&gt;
              &lt;!-- ScrollView nous permet d'avoir une page scrollable--&gt;
       
                  &lt;ScrollView Orientation=&quot;Vertical&quot;&gt;
                  
                  &lt;CollectionView Grid.Row=&quot;2&quot; Margin=&quot;25&quot; HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;FillAndExpand&quot;
                          SelectionMode=&quot;None&quot;  x:Name=&quot;AgendaCollection&quot;&gt; &lt;!--ItemsSource=&quot;{Binding AngedaCollection}&quot; --&gt;
                      &lt;CollectionView.Header&gt;
                          &lt;StackLayout Orientation=&quot;Horizontal&quot; Spacing=&quot;220&quot;&gt;
                              &lt;Label Text=&quot;Agenda&quot; TextColor=&quot;Black&quot; FontSize=&quot;18&quot;/&gt;
                              &lt;StackLayout Orientation=&quot;Horizontal&quot;&gt;
                                  
                              
                                  &lt;ImageButton Source=&quot;iconplus.png&quot;  HeightRequest=&quot;30&quot; WidthRequest=&quot;30&quot; Clicked=&quot;GoToNewFormPage&quot;&gt;&lt;/ImageButton&gt;
                                  &lt;ImageButton Source=&quot;iconmoins.png&quot;  HeightRequest=&quot;30&quot; WidthRequest=&quot;30&quot;&gt;&lt;/ImageButton&gt;
                              &lt;/StackLayout&gt;
  
  
                          &lt;/StackLayout&gt;
                          
                          
  
                      &lt;/CollectionView.Header&gt;
  
                      &lt;CollectionView.ItemsLayout&gt;
                          &lt;LinearItemsLayout Orientation=&quot;Vertical&quot; ItemSpacing=&quot;20&quot;/&gt;
                      &lt;/CollectionView.ItemsLayout&gt;
                      &lt;CollectionView.ItemTemplate &gt;
                          &lt;DataTemplate&gt;
                              
                                  &lt;pv:PancakeView HasShadow=&quot;True&quot; BackgroundColor=&quot;White&quot; VerticalOptions=&quot;StartAndExpand &quot; 
                                      HorizontalOptions=&quot;FillAndExpand&quot;&gt;
                                  &lt;Grid VerticalOptions=&quot;StartAndExpand&quot; HorizontalOptions=&quot;FillAndExpand&quot;&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                          &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
                                      &lt;BoxView BackgroundColor=&quot;{Binding Color}&quot; WidthRequest=&quot;3&quot; HorizontalOptions=&quot;Start&quot;
                                       VerticalOptions=&quot;FillAndExpand&quot;/&gt;
                                      &lt;Expander Grid.Column=&quot;1&quot; &gt;
                                          &lt;Expander.Header&gt;
                                              &lt;Grid HorizontalOptions=&quot;FillAndExpand&quot;&gt;
                                                  &lt;Grid.ColumnDefinitions&gt;
                                                      &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                                                      &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                                      &lt;ColumnDefinition Width=&quot;3.5*&quot;/&gt;
                                                  &lt;/Grid.ColumnDefinitions&gt;
                                                  &lt;StackLayout HorizontalOptions=&quot;Center&quot; VerticalOptions=&quot;Center&quot;&gt;
                                                      &lt;Label Text=&quot;{Binding Date, StringFormat='{0:dd}'}&quot; TextColor=&quot;#008A00&quot; FontSize=&quot;27&quot; 
                                                     HorizontalOptions=&quot;Center&quot;/&gt;
  
                                                      &lt;Label Text=&quot;{Binding Date, StringFormat='{0:MMMM}'}&quot; TextColor=&quot;Black&quot; FontSize=&quot;10&quot; 
                                                     HorizontalOptions=&quot;Center&quot; Margin=&quot;0,-10,0,0&quot; FontAttributes=&quot;Bold&quot;/&gt;
                                                      &lt;ImageButton Source=&quot;iconplus.png&quot; HorizontalOptions=&quot;Center&quot; HeightRequest=&quot;30&quot; WidthRequest=&quot;30&quot; Clicked=&quot;GoToFormPage&quot;&gt;&lt;/ImageButton&gt;
                                                  &lt;/StackLayout&gt;
                                                  &lt;BoxView Grid.Column=&quot;1&quot; BackgroundColor=&quot;#F2F4F8&quot; WidthRequest=&quot;1&quot; HorizontalOptions=&quot;Start&quot; 
                                                   VerticalOptions=&quot;FillAndExpand&quot;/&gt;
                                                  &lt;StackLayout x:Name=&quot;topicLayout&quot; Grid.Column=&quot;2&quot; HorizontalOptions=&quot;Start&quot; VerticalOptions=&quot;Center&quot; Margin=&quot;20&quot;&gt;
                                                      &lt;Label Text=&quot;{Binding Topic}&quot; TextColor=&quot;#008A00&quot; FontSize=&quot;15&quot; FontAttributes=&quot;Bold&quot;/&gt;
                                                      &lt;Label Text=&quot;{Binding Duration}&quot; TextColor=&quot;#2F3246&quot; FontSize=&quot;12&quot; Margin=&quot;0,-10,0,0&quot;/&gt;
                                                      &lt;ImageButton Source=&quot;iconmoins.png&quot;  HeightRequest=&quot;30&quot; WidthRequest=&quot;30&quot; Clicked=&quot;DeleteButtonClicked&quot;&gt;&lt;/ImageButton&gt;
                                                  &lt;/StackLayout&gt;
                                              &lt;/Grid&gt;
                                          &lt;/Expander.Header&gt;
                                          &lt;Grid HorizontalOptions=&quot;FillAndExpand&quot;&gt;
                                              &lt;Grid.ColumnDefinitions&gt;
                                                  &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                                                  &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                                                  &lt;ColumnDefinition Width=&quot;3.5*&quot;/&gt;
                                              &lt;/Grid.ColumnDefinitions&gt;
                                              &lt;BoxView Grid.Column=&quot;1&quot; BackgroundColor=&quot;#F2F4F8&quot; WidthRequest=&quot;1&quot; HorizontalOptions=&quot;Start&quot; 
                                               VerticalOptions=&quot;FillAndExpand&quot;/&gt;
                                              &lt;StackLayout Grid.Column=&quot;2&quot; Spacing=&quot;10&quot;&gt;
                                                  &lt;Label Text=&quot;Tâches&quot; TextColor=&quot;Black&quot; FontSize=&quot;15&quot; Margin=&quot;20,0&quot;/&gt;
                                                  &lt;StackLayout BindableLayout.ItemsSource=&quot;{Binding Speakers}&quot; HorizontalOptions=&quot;Start&quot; VerticalOptions=&quot;Center&quot; Margin=&quot;20,0,0,20&quot;&gt;
                                                      &lt;BindableLayout.ItemTemplate&gt;
                                                          &lt;DataTemplate&gt;
                                                              &lt;Label TextColor=&quot;#2F3246&quot; FontSize=&quot;12&quot;&gt;
                                                                  &lt;Label.FormattedText&gt;
                                                                      &lt;FormattedString&gt;
                                                                          &lt;FormattedString.Spans&gt;
                                                                              &lt;Span Text=&quot;{Binding Time}&quot;/&gt;
                                                                              &lt;Span Text=&quot; - &quot;/&gt;
                                                                              &lt;Span Text=&quot;{Binding Name}&quot; FontAttributes=&quot;Bold&quot;/&gt;
                                                                          &lt;/FormattedString.Spans&gt;
                                                                      &lt;/FormattedString&gt;
                                                                  &lt;/Label.FormattedText&gt;
                                                              &lt;/Label&gt;
                                                          &lt;/DataTemplate&gt;
                                                          
                                                      &lt;/BindableLayout.ItemTemplate&gt;
                                                      
                                                  &lt;/StackLayout&gt;
  
                                                  
                                              &lt;/StackLayout&gt;
                                          &lt;/Grid&gt;
                                      &lt;/Expander&gt;
                                  &lt;/Grid&gt;
                              &lt;/pv:PancakeView&gt;
                              
                          &lt;/DataTemplate&gt;
                      &lt;/CollectionView.ItemTemplate&gt;
                  &lt;/CollectionView&gt;
  
  
  
              &lt;/ScrollView&gt;
                
          &lt;/ContentPage.Content&gt;
  </code></pre>
  <p><strong>My AcceuilPage.xaml.cs</strong></p>
  <pre><code>using Calculette.ViewModel;
  using Calculette.Models;
  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Xamarin.Forms;
  using Xamarin.Forms.PancakeView;
  
  namespace Calculette
  {
      public partial class MainPage : TabbedPage
      {
          public MainPage()
          {
              InitializeComponent();
              BindingContext = new Agenda();
  
          }
          protected async void GoToFormPage(object sender, EventArgs e)
          {
              await Navigation.PushAsync(new Views.AgendaItemDetailPage());
          }
          protected async void GoToNewFormPage(object sender, EventArgs e)
          {
              await Navigation.PushAsync(new Views.NewFormPage());
          }
  
  
          async void DeleteButtonClicked(object sender, EventArgs e)
          {
              
              var agenda = (Agenda)BindingContext;
  
              await App.Database.DeleteAgendaAsync(agenda);
              await Navigation.PopAsync();
  
          }
  
          protected override async void OnAppearing()
          {
             base.OnAppearing();
              AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
          }
      }
  </code></pre>
  <p>}</p>
  <p><strong>AgendaDatabase.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using SQLite;
  using Calculette.Models;
  using System.Threading.Tasks;
  
  namespace Calculette.Database
  {
      public class AgendaDatabase
      {
          readonly SQLiteAsyncConnection database;
  
          public AgendaDatabase(string dbPath)
          {
             
              database = new SQLiteAsyncConnection(dbPath);
              database.CreateTableAsync&lt;Agenda&gt;().Wait();
          
  
  
          }
  
          // Get all agenda
          public Task&lt;List&lt;Agenda&gt;&gt; GetAgendasAsync()
          {
              return database.Table&lt;Agenda&gt;().ToListAsync();
          }
  
          // Get specific agenda
          public Task&lt;Agenda&gt; GetAgendaAsync(int id)
          {
              return database.Table&lt;Agenda&gt;()
                              .Where(i =&gt; i.ID == id)
                              .FirstOrDefaultAsync();
          }
  
          // Insert new agenda (save)
          public Task&lt;int&gt; SaveAgendaAsync(Agenda agenda)
          {
              if (agenda.ID != 0)
              {
                  return database.UpdateAsync(agenda);
              }
              else
              {
                  return database.InsertAsync(agenda);
              }
          }
          
          //Delete specific agenda
          public Task&lt;int&gt; DeleteAgendaAsync(Agenda agenda)
          {
              return database.DeleteAsync(agenda);
          }
      }
  }
  </code></pre>
  <p><strong>Agenda.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Text;
  using SQLite;
  using Calculette.Database;
  
  namespace Calculette.Models
  {
      [Table(&quot;Agenda&quot;)]
      public class Agenda
      {
          [PrimaryKey, AutoIncrement]
          public int ID { get; set; }
          public string Topic { get; set; }
          public string Duration { get; set; }
          public DateTime Date { get; set; }
  
      }
  }
  </code></pre>