检查文本框是否等于数据网格视图列中的任何值
Check if textbox equals any value from data grid view column
我想检查按下按钮时我的文本框 (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....
}
但这当然不行。
apDGV.Columns[0] 是一个 DataGridViewColumn 对象。
要检查单元格的值,请对它们进行寻址,例如像这样:
apDGV.Rows[0].Cells[0].Value
只需循环遍历列中的所有单元格并比较值。
如果要检查列中的每个值,则必须遍历列中的每一行。
此外,最好写列名而不是数字。
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);
}
}
}
我想检查按下按钮时我的文本框 (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....
}
但这当然不行。
apDGV.Columns[0] 是一个 DataGridViewColumn 对象。
要检查单元格的值,请对它们进行寻址,例如像这样:
apDGV.Rows[0].Cells[0].Value
只需循环遍历列中的所有单元格并比较值。
如果要检查列中的每个值,则必须遍历列中的每一行。 此外,最好写列名而不是数字。
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);
}
}
}