所以我有這個datagridview2,我用它來搜索。當我搜索并找到我想要的行時,我單擊該行,所有值都轉到文本框,并且有一個按鈕可以使用文本框 1 上的 id 在單擊時洗掉記錄,在另一個 datagridview1 中我有一個方法當我按其 id 洗掉該專案時,它會重繪 整個 datagridview 記錄。所以回到datagridview2,我想當我洗掉該行時,該行也從datagridview中洗掉,而不使用我創建的refilldatagridview()方法重繪 我的datagridview,當我使用dataGridView2.Rows.Remove(row);
它時沒有回圈,以及removeAt index 問題出在回圈中它永遠不會進入 if 我不知道為什么
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (row.Cells[0].Value.ToString().Equals(textBox1.Text))
{
MessageBox.Show("Match deleted");
dataGridView2.Rows.Remove(row);
}
}
uj5u.com熱心網友回復:
我不知道這是不是你要指出的。
使用 LINQ(嘗試使用列名而不是列索引,如果我使用列索引會出錯)
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Cast<DataGridViewRow>().Where(w => w.Cells["Value1"].Value.ToString() == textBox1.Text).Select(s => s.Index).FirstOrDefault());
dataGridView2.Rows.RemoveAt(dataGridView2.Rows.Cast<DataGridViewRow>().Where(w => w.Cells["Value1"].Value.ToString() == textBox1.Text).Select(s => s.Index).FirstOrDefault());
搜索:

洗掉:

只需將上面的 2 行代碼放在回圈內即可。
我希望它有幫助!
uj5u.com熱心網友回復:
我認為有幾種方法可以嘗試
#1 簡單迭代
for (int i = 0; i < dataGridView2.Rows.Count; i )
{
if (dataGridView2[i].Cells[0].Value.ToString().Equals(textBox1.Text))
dataGridView2.Rows.Remove(dataGridView2[i]);
}
#2 如果你想使用 dataTable 進行搜索
var query = dTable.AsEnumerable().Where(r => r.Field<string>(columnName) == textBox1.Text);
foreach(var row in query.ToList())
{
row.Delete();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477852.html
