WPF DataGrid 删除所选项目

发布于 2024-11-16 05:45:55 字数 484 浏览 4 评论 0原文

最近,我一直在开发一个项目,该项目以编程方式将数据导入到 WPF DataGrid 中。

我几乎完成了该项目,但我遗漏的是一个用于删除所选单元格的按钮,这就是我陷入困境的地方!

我使用 DataGrids 的基本知识编写了这段代码:

var grid = dataGrid1;
if (grid.SelectedIndex >= 0)
 {
   for (int i = 0; i <= grid.SelectedItems.Count; i++)
   {
      grid.Items.Remove(grid.SelectedItems[i]);
   };
 }

可以很好地仅删除选定的项目,就像 CurrentItem 一样,但它不会删除超过 2 个选定的项目!

我的 DataGrid 至少应包含 100 个项目。我添加了一个删除所有选项,但这也是必要的。

如果有人给我解决方案,我将不胜感激。

Recently I've been working on a project which imports data programmicaly into a WPF DataGrid.

I'm almost done with the project but the thing that I left out was a button to remove selected cells and this is where I'm stuck!

I wrote this code using my basic knowledge of DataGrids:

var grid = dataGrid1;
if (grid.SelectedIndex >= 0)
 {
   for (int i = 0; i <= grid.SelectedItems.Count; i++)
   {
      grid.Items.Remove(grid.SelectedItems[i]);
   };
 }

Works fine on removing only the item selected just like CurrentItem but it doesn't remove anymore than 2 selected items!

The DataGrid I have should at least contain a minimum of 100 items. I've added a remove all option but this is also necessary.

I'll be thankful if anyone gives me the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

云淡月浅 2024-11-23 05:45:55

通过删除所选项目,您将更改 SelectedItems 集合。您应该先复制它,然后开始删除。

By removing selected item you are changing SelectedItems collection. You should copy it first and then start removing.

谁与争疯 2024-11-23 05:45:55

这对我来说也很有效。

while (dataGrid1.SelectedItems.Count > 0){
  dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}

This also worked well for me.

while (dataGrid1.SelectedItems.Count > 0){
  dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}
土豪 2024-11-23 05:45:55

您在这里犯的错误是在循环期间删除项目,这会扰乱循环计数,因此制作一个复制网格并从中删除选定项目,然后将其与原始网格相等。
看看这个

var grid = dataGrid1;
var mygrid = dataGrid1
if (grid.SelectedIndex >= 0)
{
  for (int i = 0; i <= grid.SelectedItems.Count; i++)
  {
    mygrid .Items.Remove(grid.SelectedItems[i]);  
   };
}

grid = mygrid;

The mistake you are doing here you are removing items during loop whaich is messing with loop count so make a copy grid and remove selecteditem from it and then equlize it by the orignal one..
Check this out

var grid = dataGrid1;
var mygrid = dataGrid1
if (grid.SelectedIndex >= 0)
{
  for (int i = 0; i <= grid.SelectedItems.Count; i++)
  {
    mygrid .Items.Remove(grid.SelectedItems[i]);  
   };
}

grid = mygrid;
怕倦 2024-11-23 05:45:55

这对我有用......

while (dataGrid1.SelectedItems.Count > 0){
  dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}

This worked for me...

while (dataGrid1.SelectedItems.Count > 0){
  dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}
秋心╮凉 2024-11-23 05:45:55

使用 SelectedItem 而不是 SelectedIndex 的 while 循环

while (dataGrid1.SelectedItems.Count > 0){
  if (dataGrid1.SelectedItem == CollectionView.NewItemPlaceholder)
      dataGrid1.SelectedItems.Remove(grid.SelectedItem);
  else
      dataGrid1.Items.Remove(dataGrid1.SelectedItem);
}

A while loop using the SelectedItem instead of the SelectedIndex

while (dataGrid1.SelectedItems.Count > 0){
  if (dataGrid1.SelectedItem == CollectionView.NewItemPlaceholder)
      dataGrid1.SelectedItems.Remove(grid.SelectedItem);
  else
      dataGrid1.Items.Remove(dataGrid1.SelectedItem);
}
青衫负雪 2024-11-23 05:45:55

我和作者有同样的问题。并找到了相当漂亮的(我认为)解决方案。

所以主要问题是 SelectedItems 动态的,当你删除一行时,它会再次重新计算。

所以我的代码如下所示:

for (int i = -datagrid1.SelectedItems.Count; i < datagrid1.SelectedItems.Count; i++)
        {
            datagrid1.SelectedItems.RemoveAt(datagrid1.SelectedIndex);
        }

因此,每次 for 循环执行步骤 1 时,datagrid1.SelectedItems.Count 都会减 1,并且变量 i 会增加。

I have stack with the same problem as an author. And found quite beautiful (I think) solution.

And so the main problem is that the SelectedItems dynamic, and when you delete one row, it is recalculated again.

And so my code looks like this:

for (int i = -datagrid1.SelectedItems.Count; i < datagrid1.SelectedItems.Count; i++)
        {
            datagrid1.SelectedItems.RemoveAt(datagrid1.SelectedIndex);
        }

So, every time the for loop is doing step 1, datagrid1.SelectedItems.Count is decreased by 1, and the variable i increases.

给我一枪 2024-11-23 05:45:55
Do While dgData.SelectedItems.Count > 0
 dgData.SelectedItem.Row.Delete()
Loop
Do While dgData.SelectedItems.Count > 0
 dgData.SelectedItem.Row.Delete()
Loop
葵雨 2024-11-23 05:45:55

我的解决方案(如果您使用的是 adonet 且 autogeneratecolumns=true);

for (int i = dgv.SelectedItems.Count-1; i >= 0; i--)
{
     DataRowView dataRow = (DataRowView)dgv.SelectedItems[i];
     dataRow.Delete();
}

My solution (if you are using adonet with autogeneratecolumns=true);

for (int i = dgv.SelectedItems.Count-1; i >= 0; i--)
{
     DataRowView dataRow = (DataRowView)dgv.SelectedItems[i];
     dataRow.Delete();
}
噩梦成真你也成魔 2024-11-23 05:45:55

这对我有用......

if (DataGrid1.SelectedItem != null) 
{
  DataRowView(DataGrid1.SelectedItem).Row.Delete();
}

This worked for me...

if (DataGrid1.SelectedItem != null) 
{
  DataRowView(DataGrid1.SelectedItem).Row.Delete();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文