在网格视图中添加值之前,请检查值是否已存在 [英] Check if value already exists before adding values in grid view
本文介绍了在网格视图中添加值之前,请检查值是否已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在4个Textbox中添加datagridview中的一些行(没有绑定)。但我需要先检查它是否已经添加,然后显示一条消息,如果尚未添加,则在gridview中添加Textbox的文本值。我正在使用VS 2008和C#我搜索了很多所有可用的解决方案,但没有和我一起工作。
我试图在行上循环,但我无法工作。
I want to add some rows in datagridview (without binding) from 4 Textboxes. but I need first to check if it is already added, it yes then show a message, if not already added, then add the text value of the Textboxin the gridview. I am using VS 2008 with C# I search a lot of all available solutions but not working with me.
I tried to loop on the rows but I couldnt work.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((string)row.Cells[0].Value == txtbx1.Text && ((string)row.Cells[1].Value == txtbx2.Text.ToString() && ((string)row.Cells[2].Value == txtbx3_new.Text.ToString() && ((string)row.Cells[3].Value == txtbx4.Text.ToString()))))
{
b_found = true;
MessageBox.Show("Test");
break;
}
}
else
{
dataGridView1.Rows.Add(new object[] { txtbx1.Text, txtbx2.Text, txtbx3.Text, txtbx4.Text, false });
}
推荐答案
private void button1_Click(object sender, EventArgs e)
{
bool entryFound = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
object val1 = row.Cells[0].Value;
object val2 = row.Cells[1].Value;
object val3 = row.Cells[2].Value;
object val4 = row.Cells[3].Value;
if (val1 != null && val1.ToString() == textBox1.Text &&
val2 != null && val2.ToString() == textBox2.Text &&
val3 != null && val3.ToString() == textBox3.Text &&
val4 != null && val4.ToString() == textBox4.Text)
{
MessageBox.Show("Entry already exist");
entryFound = true;
break;
}
}
if (!entryFound)
{
dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
}
}
这篇关于在网格视图中添加值之前,请检查值是否已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文