WPFを学習し、Windowsフォームから来てください!sqliteの行を削除するチェックボックス付きリストビューですか? WPF、C#
現在、データベースを読み込むために以下の作業を行っていますが、チェックボックスを選択したイベントにlistviewのアイテムをバインドし、ボタンをクリックした後にsqliteデータベースとlistviewから選択した行を削除する方法も知りたいと思います。
編集:AVK Naiduの助けを借りて、それは今働いています!
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();
}
ここにXAMLでの私のリストビューは、データベースをロードするためです:
<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>
データベースから削除します
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());
}
}
}
テンプレートでリストビューを作成して、4つのキーを表示しましたか?はいの場合は、そのコードを投稿できますか? – AVK
@AVKNaidu申し訳ありません、それを追加しました! – DropItLikeItsHot