如何删除具有带有循环的文本框文本值的DataGridView行?

发布于 2025-01-29 07:11:08 字数 621 浏览 4 评论 0原文

因此,我有这个DataGridView2,我正在使用它进行搜索。 当我搜索并找到我想要的行时,我单击该行,所有值都转到文本框,并且有一个按钮删除记录,请单击使用文本框1上的ID, 在另一个DataGridView1中,我有一种方法可以刷新整个DataGridView记录,当我通过其ID删除该项目时。 因此,返回到datagridview2,我想要删除该行时,该行也会从datagridview中删除,而无需使用我创建的RefillDatagridView()方法,而无需使用和刷新我的dataGridView 当我使用datagridview2.rows.remove(row);时 它可以无循环以及拆卸索引 问题在于循环中,它永远不会进入IF,我不知道为什么

foreach (DataGridViewRow row in dataGridView2.Rows)
{
    if (row.Cells[0].Value.ToString().Equals(textBox1.Text))
    {
        MessageBox.Show("Match deleted");
        dataGridView2.Rows.Remove(row);                                
    }
}

so i have this datagridview2 that i'm using it for searching.
and when i search and find the row that i want i click on that row, and all the values go to textboxes, and there is a button that deletes the record on click using the id on textbox 1,
in another datagridview1 i have a method that refreshes the whole datagridview records when i delete that item by its id.
so back to datagridview2, i want when i delete that row, the row gets deleted also from datagridview, without using and refreshing my datagridview with my refilldatagridview() method that i created,
when i use dataGridView2.Rows.Remove(row);
it works without loop, as well as the removeAt index
the problem is in the loop it never enters the if and i don't know why

foreach (DataGridViewRow row in dataGridView2.Rows)
{
    if (row.Cells[0].Value.ToString().Equals(textBox1.Text))
    {
        MessageBox.Show("Match deleted");
        dataGridView2.Rows.Remove(row);                                
    }
}

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

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

发布评论

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

评论(2

最美不过初阳 2025-02-05 07:11:08

我不知道这是否是您指出的。

使用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行代码放在循环的内部即可。

我希望它有帮助!

I don't know if this is what you're pointing out.

Using LINQ (try column name instead of column index, it's getting error if I use column index)

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());

Searching:
enter image description here

Deleting:
enter image description here

Just place the 2 line of code above inside of your loop.

I hope it helps!

岁月染过的梦 2025-02-05 07:11:08

我认为有几种方法可以尝试使用

#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();
}

I think there have Several way that u can try

#1 simple iteration

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 If u want to use dataTable to search

var query = dTable.AsEnumerable().Where(r => r.Field<string>(columnName) == textBox1.Text);
foreach(var row in query.ToList())
{
       row.Delete();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文