如何删除多行 - DataGridView()? [英] How To Delete Multiple Rows - DataGridView()?

查看:76
本文介绍了如何删除多行 - DataGridView()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是VS2010。 C#。 Windows窗体应用程序..

当我想删除多行时,我使用下面的代码,但它只是删除一个第一个选定的行而不是所有选中的多行的
..



有人能帮帮我吗?



Hi, Iam using VS2010. C#. Windows Forms Applications..
When I wish to delete multiple rows, Iam using the below codes, but It's
deleting a single first selected row only and not all selected multiple rows..

Does anybody can help me?

for each (DataGridViewRow^ Tw in myDataGrid1->Rows){
    if(Tw->Selected==true){
        //DJDtb->Rows[myDataGrid1->CurrentRow->Index]->Delete();
        myDataGrid1->Rows->Remove(Tw);
    }
}





谢谢



Thanks

推荐答案


如果您正在寻找C#的方法,下面的解决方案是太容易解决方案:



Hi If you are looking a way in C# the below solution is too much easy solution:

private void buttonRemove_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
        {
            if (oneCell.Selected)
                dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
        }
    }





但是在C ++中就是这样,但是在C#版本中你也可以使用它。 SelectedRows也是:





But in C++ it is like this, however in C# version you can use also .SelectedRows too:

foreach (DataGridViewRow row in DataGridView1.SelectedRows) 
{
    DataGridView1.Rows.Remove(row);
}





您也可以按照以下有用链接:

https://www.youtube.com/watch?v=ElCHMYjRFO0 [ ^ ]

http://stackoverflow.com/questions/2084346/how-to-delete-a-selected-datagridviewrow-和更新连接数据库表 [ ^ ]

https://social.msdn.microsoft.com/forums/windows/en -us / 3493f264-58d4-45c4-a1a8-575a09a9b9cd /删除-A-选择-R ow-in-datagrid-view [ ^ ]



Also you can follow the below useful links:
https://www.youtube.com/watch?v=ElCHMYjRFO0[^]
http://stackoverflow.com/questions/2084346/how-to-delete-a-selected-datagridviewrow-and-update-a-connected-database-table[^]
https://social.msdn.microsoft.com/forums/windows/en-us/3493f264-58d4-45c4-a1a8-575a09a9b9cd/deleting-a-selected-row-in-datagrid-view[^]


那不是C# - 它是C ++。



两者不可互换。



在C#中,禁止操作:你不能改变列表foreach循环正在迭代,我会假设您的代码在尝试时抛出异常。
That's not C# - it's C++.

The two are not interchangeable.

In C#, that operation is forbidden: you cannot alter the list that a foreach loop is iterating through, and I would assume that your code is throwing an exception when you try.


这篇关于如何删除多行 - DataGridView()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆