Reputation: 159
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());
}
}
}
Upvotes: 0
Views: 416
Reputation: 3923
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
.
Upvotes: 1