[英]Removing Selected rows in a datagridview in C#?
我目前正在使用此代碼:
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(item.Index);
}
我在第一列中有復選標記,但是使用此代碼,它只會被選中。 我如何獲取選中的CheckBoxes 只能刪除行?
你想要的東西
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i]
.Cells[yourCheckBoxColIndex].Value) == true)
{
dataGridView1.Rows.RemoveAt(i);
}
}
我希望這有幫助。
嘗試這樣的事情:
foreach(DataGridViewRow row in this.dataGridView1.Rows)
{
var checked = Convert.ToBoolean(row.Cells[0].Value); // Assuming the first column contains the Checkbox
if(checked)
dataGridView1.Rows.RemoveAt(row.Index);
}
它可能是這樣的......這是listview的一個例子,但概念幾乎就在那里。 循環瀏覽項目並找到復選框ID並刪除所選項。 希望這可以幫助。
public void btnDeleteClick(object sender, EventArgs e)
{
// Iterate through the ListViewItem
foreach (ListViewItem row in ListView1.Items)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("cbxID");
if (cb != null && cb.Checked)
{
// ListView1.DataKeys[item.DisplayIndex].Values[0].ToString()
try
{
}
catch (Exception err)
{
}
}
}
}
試試這個:
if (dgv.SelectedRows.Count>0)
{
dgv.Rows.RemoveAt(dgv.CurrentRow.Index);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.