首页 > 解决方案 > 检查文本框是否等于数据网格视图列中的任何值

问题描述

我想检查按下按钮时我的文本框 () 的值是否等于我的数据网格视图 ( ) 中apTB某个列 ( column 0( )) 中当前的任何值。alphapapaapDGV

当前代码:

private void APButton_Click(object sender, EventArgs e)
        {
            if (apTB.Text == apDGV.Columns[0])
            {
                MessageBox.Show("Duplicate.", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                stuff....
            }

但这当然行不通。

标签: c#datagridviewtextbox

解决方案


如果要检查列中的每个值,则必须遍历列中的每一行。此外,最好写列名而不是数字。

    private void Button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows) {
            if (textBox2.Text == row.Cells["columnName"].Value.ToString())
            {
                MessageBox.Show("Duplicate.", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }

推荐阅读