简体   繁体   English

从GridView删除行

[英]Deleting Rows from GridView

I have an RadGridView . 我有一个RadGridView When users clicks on a row and then click my Delete button I call a method to delete that row from my database. 当用户单击row ,然后单击“ Delete按钮时,我调用了一种从数据库中删除该行的方法。 But it shows this entry on the GridView . 但它会在GridView上显示此条目。 I can manually call the LoadGridView() method again to show the user that the entry has been deleted but isn't there any other built-in method that can remove that item from the GridView too? 我可以再次手动调用LoadGridView()方法,以向用户显示该条目已被删除,但是没有其他任何内置方法也可以从GridView中删除该项目吗?

Here is the Delete Click EventHandler: 这是Delete Click EventHandler:

private void OnDeleteClick(object sender, RoutedEventArgs e)
{
    Product deleteProduct = new Product();
    foreach (Product product in this.grdProductGrid.SelectedItems)
    {
        deleteProduct = product;
    }
    if (deleteProduct != null)
    {
        _service.DeleteProductAsync(deleteProduct);
        this.grdProductGrid.SelectedItems.Clear();
    }
}

The grid binding: 网格绑定:

List<Product> productList = new List<Product>();
this.grdProductGrid.ItemsSource = productList;

Since you're using a List as a source, you need to remove the item from the list and manually force the RadGridView to refresh; 由于您使用List作为源,因此需要从列表中删除该项目,然后手动强制RadGridView刷新。

// Temporarily store the ItemSource as a List in tmp
List tmp = (List)this.grdProductGrid.ItemsSource; 

// Remove the item from the List
tmp.Remove(deleteP‌​roduct); 

// Force a refresh by "tricking" RadGridView 
// that it's getting an entirely new ItemsSource
this.grdProductGrid.ItemsSource = null; 
this.grdProductGrid.ItemsSource = tmp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM