0

Learning WPF and coming from Windows forms!

I am currently doing the following to load a database, however I'd also like to know how to bind checkboxes to the selected event for listview's items and delete the selected row from a sqlite database and listview after a button click.

Edit: thanks to the help of AVK Naidu, it's now working!

   public ObservableCollection<MyItem> myItems { get; set; }
   public class MyItem
    {
        public string Key { get; set; }
        public string Key1 { get; set; }
        public string Key2 { get; set; }
        public string Key3 { get; set; }
        public bool IsSelected { get; set; }
    }
    private void LoadDatabaseButton_Click(object sender, RoutedEventArgs e)
    {
        myItems = new ObservableCollection<MyItem>();
        SQLiteConnection m_dbConnection;
        m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
        m_dbConnection.Open();
        SQLiteCommand readdatabase = new SQLiteCommand("Select * From TableName", m_dbConnection);
        using (SQLiteDataReader read = readdatabase.ExecuteReader())
        {
            while (read.Read())
            {
                listView4.Items.Add(new MyItem { Key = read["Key"].ToString(), Key2 = read["Key2"].ToString(), Key3 = read["Key3"].ToString(), Key4 = read["Key4"].ToString() });
            }
        }
        m_dbConnection.Close();
    }

Here's my listview in XAML for loading the database:

<ListView x:Name="listView4" HorizontalAlignment="Left" Height="186" VerticalAlignment="Top" Width="432" BorderBrush="Gray">
    <ListView.View>
        <GridView>
          <GridViewColumn Header="Key" DisplayMemberBinding="{Binding Key}"/>
          <GridViewColumn Header="Key1" DisplayMemberBinding="{Binding Key1}"/>
          <GridViewColumn Header="Key2" DisplayMemberBinding="{Binding Key2}"/>
          <GridViewColumn Header="Key3" DisplayMemberBinding="{Binding Key3}"/>

        </GridView>
    </ListView.View>
      </ListView>

Delete from database:

        private void RemoveRowButton_click(object sender, RoutedEventArgs e)
    {

        foreach (var Checkboxitem in myItems)
        {
                if (Checkboxitem.IsSelected == true)
                {
                    MessageBox.Show(Checkboxitem.Key.ToString() + Checkboxitem.Key1.ToString() + Checkboxitem.Key2.ToString() + Checkboxitem.Key3.ToString());
                }

        }
    }
2
  • 1
    Did you already create a listview with your template to show your 4 Keys? If yes, can you post that code? Commented Oct 4, 2016 at 17:10
  • @AVKNaidu Alright, added it! Commented Oct 4, 2016 at 17:19

1 Answer 1

1

Change your MyItem to below.

public class MyItem
{
    public bool IsSelected { get; set; }
    public string Key { get; set; }
    public string Key1 { get; set; }
    public string Key2 { get; set; }
    public string Key3 { get; set; }
}

In your GridView add a CheckBox Column. Your ListView will be something like Below.

<ListView x:Name="listView4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Gray">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Key" DisplayMemberBinding="{Binding Key}"/>
            <GridViewColumn Header="Key1" DisplayMemberBinding="{Binding Key1}"/>
            <GridViewColumn Header="Key2" DisplayMemberBinding="{Binding Key2}"/>
            <GridViewColumn Header="Key3" DisplayMemberBinding="{Binding Key3}"/>               
        </GridView>
    </ListView.View>
</ListView>

If you look at the CheckBox from GridViewColumn.CellTemplate, I made the Mode=TwoWay so that it can update the collection from which you are binding the data to.

So for a sample data like below,

myItems = new ObservableCollection<MyItem>();
for (int i = 0; i < 20; i++)
{
    myItems.Add(new MyItem() { Key = i.ToString(), Key2 = i.ToString(), Key1 = i.ToString(), Key3 = i.ToString(), IsSelected = false });
}
listView4.ItemsSource = myItems;

If you revisit your myItems on Delete Button Click, you can see that your actual collection's IsSelected will be updated to true if the checkbox is checked. Now you can iterate through your Collection and delete those records where IsSelected is true.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, lots of good information here. I am trying to get it to work, and using what you mentioned but it's not werkin'. I updated my example :) Right now when I actually go to show a checked item in message box, I get "ProjectName.MainWindow+MyItem".
Change it to Checkbox.Key.ToString()
I must've rearranged everything 20 times before I noticed that. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.