2016-10-04 14 views
0

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()); 
       } 

     } 
    } 
+1

テンプレートでリストビューを作成して、4つのキーを表示しましたか?はいの場合は、そのコードを投稿できますか? – AVK

+0

@AVKNaidu申し訳ありません、それを追加しました! – DropItLikeItsHot

答えて

1
があなたの MyItem以下に変更し

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; } 
} 

GridViewにチェックボックス列を追加します。 ListViewは以下のようなものになります。

<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> 

あなたはGridViewColumn.CellTemplateからCheckBoxを見れば、それはあなたがにデータを結合しているから、コレクションを更新できるように、私はモード=双方向を作りました。以下のようなサンプルデータのためのそう

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; 

[削除]ボタンをクリックの上ごmyItemsを再検討する場合は、チェックボックスがチェックされている場合は、あなたの実際のコレクションのIsSelectedtrueに更新されることがわかります。コレクションを繰り返し処理して、IsSelectedtrueのレコードを削除できます。

+0

ありがとう、ここにたくさんの良い情報があります。私はそれを働かせようとしています、あなたが言いましたが、それは魔女ではありません。私は私の例を更新:)今すぐ実際にメッセージボックスにチェックされた項目を表示するために行くと、私は "ProjectName.MainWindow + MyItem"を取得します。 – DropItLikeItsHot

+1

Checkbox.Key.ToString()に変更してください – AVK

+0

私はそれを気付く前に20回すべてを整理しておく必要があります。どうもありがとうございました! – DropItLikeItsHot

関連する問題