我有以下问题。我有一个GridView,它在我的数据库中通过以下代码填充:
private async void CreateDatabase()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people");
var query = conn.Table<Person>();
var result = await query.ToListAsync();
foreach (var item in result)
{
GridView1.Items.Add(new GridViewItem { Background = new SolidColorBrush(Colors.Green), Content = item.Name, Width = 200, Height = 200 });
}
}
我从我的数据库中删除了这样的项目:
private async void DeleteSelectedButton_Click(object sender, RoutedEventArgs e)
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people");
var query = conn.Table<Person>().Where(x => x.Name == "123");
var result = await query.ToListAsync();
foreach (var item in result)
{
await conn.DeleteAsync(item);
}
}
我的问题是:我可以将哪个特定值绑定到第一个代码块中创建的GridView1.Item,这样我可以在以后检索它以在选中此项时删除数据库中的实体? (即1.填充GridView时,我希望为每个GridView.Item分配一些绑定到我的数据库中ID的值; 2.我希望能够通过右键单击它然后按下来选择项目时检索此值删除按钮(这样就不会删除名称中包含&#34; 123&#34;的数据库实体,而是绑定了该项目的ID的实体)。任何帮助都会非常感谢!
P.S。抱歉我的复杂句子。
答案 0 :(得分:2)
如果你的网格只显示一个字符串,你可以得到这样的GridView值:
string value = Gridview1.SelectedItem as string;
答案 1 :(得分:0)
好的)这是交易)我弄清楚了:
private async void RemoveButton_Click(object sender, RoutedEventArgs e)
{
GridViewItem selection = (GridViewItem)GridView1.SelectedItem;
string ToBeDeleted = selection.Content.ToString();
GridView1.Items.Remove(selection);
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people");
var query = conn.Table<Person>().Where(x => x.Name == ToBeDeleted);
var result = await query.ToListAsync();
foreach (var item in result)
{
await conn.DeleteAsync(item);
}
MessageDialog msgDialog = new MessageDialog("Category " + ToBeDeleted + " is deleted");
msgDialog.ShowAsync();
}