简体   繁体   English

检查文本框是否等于数据网格视图列中的任何值

[英]Check if textbox equals any value from data grid view column

I want to check if the value of my textbox ( apTB ) at the time of a button press equals any value currently in a certain column ( column 0 ( alphapapa )) in my data grid view ( apDGV ).我想检查按下按钮时我的文本框( apTB )的值是否等于我的数据网格视图( apDGV )中某一列(第column 0alphapapa ))中当前的任何值。

Current Code:当前代码:

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

But it is of course not working.但这当然行不通。

If you want to check every value in column, you must iterate through every row in column.如果要检查列中的每个值,则必须遍历列中的每一行。 Additionally, it is good practice to write column names instead of numbers.此外,最好写列名而不是数字。

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

apDGV.Columns[0] is a DataGridViewColumn object. apDGV.Columns[0]是一个 DataGridViewColumn object。

To check the value of cells, adress them eg like this:要检查单元格的值,请像这样对它们进行寻址:

apDGV.Rows[0].Cells[0].Value

Just iterate with a loop over all the cells in your column and compare the values.只需循环遍历列中的所有单元格并比较值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM