2

can you please help me on displaying the searched row in message box?

I've below code for searching a value in the row.

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;
        }
    }
}
4

2 回答 2

2

以下:

MessageBox.Show("foo")

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

您可以在此链接上了解有关该方法及其重载的更多信息。

string以愉快的编码方式获取您想要的信息。

于 2013-06-19T00:26:14.203 回答
2

如果您要做的是Name在 a 中显示列的值,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.
        }
    }
}
于 2013-06-19T00:32:00.143 回答