我有dataGridView1
,用户可以输入信息,然后点击button3
我希望用户搜索textBox3
中输入的内容并获取MessageBox
说明datagridview
中是否找到字符串。
我的代码是
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!!");
}
答案 0 :(得分:0)
没有看到一些实际的代码很难具体,这就是我要做的事情:
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);
}
}
完全未经测试,基本上您希望执行以下操作:
玩得开心。
答案 1 :(得分:-1)
bool found = false;
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (found == false)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (row.Cells[row.Cells.IndexOf(cell)].Value.ToString() == (textBox3.Text))
{
MessageBox.Show("Yeeeeeees");
found = true;
break;
}
else
{
}
}
}
}
}
catch (Exception)
{
}
if (found == false)
{
MessageBox.Show("Nooooooope");
}