如何使用C#2010中的删除按钮删除无界DataGridView中的选定行和整行? [英] How to Delete selected rows and Entire rows in a Unbounded DataGridView using a Delete Button in C# 2010?
问题描述
在我的项目中我正在使用无界DataGridView。其中我使用标题复选框将第一行作为CheckBoxColumn。我提供了我的最终用户在DataGridView中选择单行或多行或整行,并且可以在需要时删除它。但真正的问题是,当我选择所有行并单击删除时,它会删除整行,而当我选择一行时,它会执行相同的操作。
以下是我的删除按钮代码。
In My Project I''m Using a Unbounded DataGridView . In Which I made First Row as CheckBoxColumn With Header Check Box . I Provided My End User to select a single row or multiple row or Entire rows in DataGridView and can delete it if they need it . But the real Problem is When I Select all rows and click delete it deletes Entire rows and while i select a single row it performs the same action.
Below is my delete Button code .
private void BtnDcDelete_Click(object sender, EventArgs e)
{
do
{
foreach (DataGridViewRow row in dataGridViewEx1.Rows)
{
try
{
dataGridViewEx1.Rows.Remove(row);
}
catch (Exception) { }
}
} while (dataGridViewEx1.Rows.Count > 1);
}
虚假大而大胆删除,代码块添加 - OriginalGriff [/ edit]
[edit]Spurious big and bold removed, Code block added - OriginalGriff[/edit]
推荐答案
如果您只想删除选定的行,则需要使用SelectedRows属性:
If you want to delete just the selected rows, then you need to use the SelectedRows property:
foreach (DataGridViewRow row in myDataGridView.SelectedRows)
{
myDataGridView.Rows.Remove(row);
}
这篇关于如何使用C#2010中的删除按钮删除无界DataGridView中的选定行和整行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!