I have a dataGridView1
and the user can enter info to it, then by clicking a button3
I want the user to search for whatever he types in a textBox3
And to get a MessageBox
saying if the string was found or not in the datagridview
.
My code is
bool exists = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (exists == true)
{
break;
}
else
{
for (int k = 1; k < dataGridView1.Rows[i].Cells.Count; k++)
{
if (textBox3.Text == dataGridView1.Rows[i].Cells[k].Value.ToString())
{
exists = true;
break;
}
}
}
}
if (exists == true)
{
MessageBox.Show("It exists!");
}
else
{
MessageBox.Show("It doesn't exist!!");
}
Without seeing some actual code its hard to get specific, here's what I would do:
private void button3_Click(object sender, EventArgs e)
{
string SearchString = textBox3.text;
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[SearchColumn.SelectedIndex].Value.ToString().Equals(SearchString))
{
MessageBox.Show("Match Found");
//This will only pick up the first match not multiple…
}
else
{
MessageBox.Show("No Match");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Completely untested, essentially you want to be doing the following:
Have fun.