【问题标题】:How to scan through values / rows in DataGridView如何扫描 DataGridView 中的值/行
【发布时间】:2017-03-14 09:34:30
【问题描述】:

这其实是我第一次问。我试图搜索所有内容,但找不到答案。

我想从一开始就扫描我在 DataGridView 中的所有值以防止重复。我使用 Access 作为我的数据库。到目前为止,我只能检查选定的行,然后将其与文本框进行比较。 My current form.如果文本框值在所有数据中具有相似的项目名称,我需要禁用“添加”

这是我的代码。它只影响选定的行。不是所有的行。

string item = dgvInventory.Rows[dgvInventory.CurrentRow.Index].Cells[0].Value.ToString();
            if (tbItem.Text == item)
            {

                btnAdd.Enabled = false;
                lblAdd.Text = "Item name cannot be the same";
            }
            else
            {
                btnAdd.Enabled = true;
                lblAdd.Text = "Accepted";
            }

请帮忙?

我试图插入一个for 循环所以这是我现在的代码,但它只发生在最后一行。我在这里想念什么?或者我犯了什么错误

for (int x = 0; x < dgvInventory.Rows.Count; x++)
            {
                string item = dgvInventory.Rows[x].Cells[0].Value.ToString();
                if (tbItem.Text == item)
                {

                    btnAdd.Enabled = false;
                    lblAdd.Text = "Item name cannot be the same";
                }
                else
                {
                    btnAdd.Enabled = true;
                    lblAdd.Text = "Accepted";
                }
            }

【问题讨论】:

  • 你可以做一个for 循环然后在那里添加条件。像这样for (int i = 0; i &lt; dataGridView1.Rows.Count; i++)
  • 有很多方法可以做到这一点:for 循环,foreach 循环,Linq...
  • @P.Pat 谢谢你的建议。我试过for (int x = 0; x &lt; dgvInventory.Rows.Count; x++) 然后string item = dgvInventory.Rows[x].Cells[0].Value.ToString(); 不知何故它只适用于最后一行
  • 调试代码,你会发现它只在最后一行不起作用。问题是你在每一行上设置item,所以最后你得到了最后一行的值。您需要检查值是否与您在 for 循环中查找的值相同

标签: c# database ms-access datagridview


【解决方案1】:

您可以这样做,在RowsDataGridView 的集合上使用foreachloop:

bool found = false;
foreach (DataGridViewRow dgvr in dgvInventory.Rows)
{
    if (tbItem.Text == dgvr.Cells[0].Value.ToString())
    {
        found = true;
        break;
    }
}

if (found)
{
       btnAdd.Enabled = false;
       lblAdd.Text = "Item name cannot be the same";
}
else
{
       btnAdd.Enabled = true;
       lblAdd.Text = "Accepted";
}

【讨论】:

  • 这行得通!。但我必须编辑一些东西,比如dgvr.Cells[0].Value.toString() 但你基本上解决了它
  • 是的,我忘记了ToString。很高兴它有帮助:)
【解决方案2】:

编辑:注意:如果这是您要查找的内容,但不完整,我可以指导/完成代码

使用:

public void SetUpdatable()
{
    var canUpdate = VerifyAllRows(tbItem.Text);
    if (canUpdate)
    {
        btnAdd.Enabled = true;
        lblAdd.Text = "Accepted";
    }
    else
    {
       btnAdd.Enabled = false;
       lblAdd.Text = "Item name cannot be the same";
    }
}

验证所有行:

public bool VerifyAllRows(string pValue)
{
    foreach (DataGridViewRow row in dgvInventory.Rows)
    {
        if (!VerifyRow(row,pValue))
        {
            return false;
        }
    }
    return true;
}

验证行:

public bool VerifyRow(DataGridViewRow pRow,string pValue)
{
    foreach (DataGridViewCell cell in pRow.Cells)
    {
        string currentItem = cell.Value.ToString();
        if (currentItem == pValue)
        {
            return false;
        }
    }
    return true;
}

【讨论】:

  • 我猜这不是 OP 想要的。这会检查所选行中的列,OP 想要检查每一行中的列
  • 是的,这是一般的想法 - 但是一旦你有一个测试行的方法,迭代它来测试所有的行 - 如果它是 OP 正在寻找的,我可以指导/完成代码。
  • 感谢您的建议,但我试图扫描所有行。并检查文本框是否在 datagridview 中已有相同的文本
  • @OriNachum 是的。我找不到测试/检查所有行的方法。我只能管理选定的行,我不知道如何。我被困了很长时间
  • 这有意义吗?如果您有权访问网格,而不仅仅是特定行,则可以遍历行。编辑:请注意,使用 [0] 仅测试行中的第一个单元格。 (除非那是你想要的)
猜你喜欢
  • 2015-11-23
  • 2014-02-14
  • 2021-05-18
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 2012-04-21
相关资源
最近更新 更多