【发布时间】:2020-03-21 18:47:50
【问题描述】:
我想删除一个选定的行,我不认为我的代码离我很远。目前,它会删除保存在 SQLite DB 上的列表视图中的所有行,这对于删除所有按钮非常有用,但我不希望这样。以下是我的代码:
void SaveButton_Clicked(object sender, System.EventArgs e)
{
MainWindowViewModel APR = new MainWindowViewModel()
{
ProductName = proName.Text,
TotalAPR = totAPR.Text,
Total = tot.Text,
Monthly = mon.Text
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
int rowsAdded = conn.Insert(APR);
}
DisplayAlert("Saved!", "Your APR has been saved!", "OK");
BindingContext = new MainWindowViewModel();
OnAppearing();
}
void RemoveButton_Clicked(object sender, System.EventArgs e)
{
MainWindowViewModel APR = new MainWindowViewModel()
{
ProductName = proName.Text,
TotalAPR = totAPR.Text,
Total = tot.Text,
Monthly = mon.Text
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.DeleteAll<MainWindowViewModel>();
int rowsDeleted = conn.Delete(APR);
}
DisplayAlert("Deleted!", "This saved APR has been deleted!", "OK");
BindingContext = new MainWindowViewModel();
OnAppearing();
}
protected override void OnAppearing()
{
base.OnAppearing();
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<MainWindowViewModel>();
var APRS = conn.Table<MainWindowViewModel>().ToList();
APRListView.ItemsSource = APRS;
}
}
我也试过了:
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.Delete<MainWindowViewModel>();
int rowsDeleted = conn.Delete(APR);
}
但我被告知根据 Intellisense 将其更改为 conn.DeleteALL。
【问题讨论】:
-
用户从列表中选择一个项目,然后单击删除按钮?还是列表中的每个项目都有自己的删除按钮?
-
@Jason 用户从列表中选择一个项目,然后单击删除按钮
-
@Jason 我试过标准的 SelectedItem 但智能感知不喜欢它
-
您正在调用 DeleteAll() 然后 Delete() - 这当然会删除所有内容。当您的列表中已经有了新的虚拟机时,也没有理由创建新的虚拟机。最后,你的 MainWindowViewModel 有 PK 吗?