C#WPF DataGrid删除每个选定的项目

时间:2018-02-12 11:12:38

标签: c# wpf datagrid

我有一个数据网格,在xaml中被称为" DataGridUsers"我想删除所选的值但我该怎么做?

这是我的代码

    private void BtnDelete_Click(object sender, RoutedEventArgs e)
    {
        if (DataGridUsers.SelectedItem == null)
        {
            MessageBox.Show("There is no selected rows!");// show a message here to inform
        }
        else
        {
            foreach (DataGrid item in DataGridUsers.SelectedItems)
            {
                DataGridUsers.Items.Remove(item);
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

使用SelectedIndex

while (DataGridUsers.SelectedItems.Count > 0){
  DataGridUsers.Items.RemoveAt(DataGridUsers.SelectedIndex);
}

答案 1 :(得分:0)

试试这个:

var obj = {
   boxer: [],
   brabancon: [],
   briard:  [],
   bulldog: [ "boston", "french" ]
}

var arr = Object.keys(obj);
var newArr =[];

arr.forEach(function(element) {
  console.log(element);
  newArr.push({'breed':element})
});