c# - 检查文本框是否等于数据网格视图列中的任何值
问题描述
我想检查按下按钮时我的文本框 () 的值是否等于我的数据网格视图 ( ) 中apTB
某个列 ( column 0
( )) 中当前的任何值。alphapapa
apDGV
当前代码:
private void APButton_Click(object sender, EventArgs e)
{
if (apTB.Text == apDGV.Columns[0])
{
MessageBox.Show("Duplicate.", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
stuff....
}
但这当然行不通。
解决方案
如果要检查列中的每个值,则必须遍历列中的每一行。此外,最好写列名而不是数字。
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);
}
}
}
推荐阅读
- python - TypeError:scatter()缺少1个必需的位置参数:Plotly中的'data_frame'
- java-8 - 如何使用completionStage编写while循环,最终返回CompletionStage而不加入?
- java - java swing中的按钮有问题
- sql-server - EF 6.1.3 异常 - 无法将“System.Data.Entity.Core.Objects.ObjectContext”类型的对象转换为“System.Data.Entity.DbContext”类型
- excel - 如何创建表的超链接?
- airflow - 气流 - 发送失败短信通知
- jenkins - 通过 ec2-plugin 以编程方式启动额外的 Jenkins 节点
- python - “来自通配符导入的未使用的导入操作系统”
- python-3.x - 使用 Dropbox API 和 python 列出文件夹和子文件夹中的所有文件
- try-catch - 尝试无例外