在单击C#按钮后,如何从DataGridView中选择一行等于textbox.Text?

时间:2014-09-19 18:52:43

标签: c# datagridview

如何在单击C#按钮后从DataGridView等于textbox.Text中选择一行?还有其他解决方案吗?

private void btnChk(object sender, EventArgs e)
    {
        for (int i = 0; i < dgv.Rows.Count; i++)
        {
            if (dgv.Rows[i].Cells[1].Value.ToString()==txtName.Text)
            {
               dgv.CurrentCell = dgv.Rows[i].Cells[1];

            }

        }
    }

2 个答案:

答案 0 :(得分:1)

您也可以

private void btnChk(object sender, EventArgs e)
{
    for (int i = 0; i < dgv.Rows.Count; i++)
    {
        if (dgv.Rows[i].Cells[1].Value.ToString()==txtName.Text)
        {
           dgv.Rows[i].Cells[1].Selected = true;
        }

    }
}   

答案 1 :(得分:1)

也是答案

private void btnChk(object sender, EventArgs e)
{
    for (int i = 0; i < dgv.Rows.Count; i++)
    {
        if (dgv.Rows[i].Cells["Name"].Value.ToString()==txtName.Text)
        {
           dgv.Rows[i].Cells["Name"].Selected = true;
        }

    }
}