如何在消息框中显示搜索到的行

时间:2013-06-19 00:20:04

标签: c# messagebox

你可以帮我在消息框中显示搜索到的行吗?

我在下面的代码中搜索行中的值。

private void button3_Click(object sender, EventArgs e)
{
        // Code to search the  alphanumneric Part Number (in Column1 header called "Name") and highlihgt the row

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells["Age"].Value.ToString().Equals(textBox3.Text.StringComparison.CurrentCultureIgnoreCase))   
        {  
            dataGridView1.Rows[row.Index].Selected = true;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

以下内容:

MessageBox.Show("foo")

将在Windows窗体应用程序中显示带有文本foo的消息框(我想这就是您所拥有的)。

您可以详细了解该方法及其重载on this link

string和快乐的编码获取您想要的信息。

答案 1 :(得分:2)

如果您要执行的操作是在Name中显示MessageBox列的值,请执行以下操作:

private void button3_Click(object sender, EventArgs e)
{
    // Code to search the  alphanumneric Part Number (in Column1 header called "Name") and highlihgt the row

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells["Age"].Value.ToString().Equals(textBox3.Text.StringComparison.CurrentCultureIgnoreCase))   
        {  
            dataGridView1.Rows[row.Index].Selected = true;
            MessageBox.Show(row.Cells["name"].Value.ToString());
            break;  //This exits the `foreach` loop - not necessary just an assumption.
        }
        else
        {
            //Do something if you don't find what you wanted or `continue` if you want the loop to keep going.
        }
    }
}