簡體   English   中英

C#在DataGridView中搜索列,並選中該行

[英]C# search column in a DataGridView and make that row selected

我需要可以提供DataGridView ,列號和字符串以進行搜索的函數,並且它基於要搜索的列和要搜索的字符串選擇該DataGridView行。 我有下面的基本函數,只是缺少一些代碼來使其工作。 有什么想法嗎?

public void dgvSearchSetCurrent(DataGridView oDataGrid, int iCol, string sSearch)
{
  if (oDataGrid.RowCount > 0)
  {
    bool iFound = false;
    //Search DG column and set select/set the current row
    //missing code here

    //if not found, set current row 0
    if (iFound == false)
    {
      oDataGrid.Rows[0].Selected = true;
    }
  }
}
    public void dgvSearchSetCurrent(DataGridView oDataGrid, int iCol, string sSearch)
    {
        if (oDataGrid.RowCount > 0)
        {
            bool iFound = false;
            //Search DG column and set select/set the current row

            for(int i = 0 ; i < oDataGrid.Rows.Count ; i++)
            {
                if(oDataGrid.Rows[i].Cell[iCol].Value.ToString() == sSearch)
                //if(oDataGrid.Rows[i].Cell[iCol].Value.ToString().Contains(sSearch))
                {
                    iFount = true;
                    oDataGrid.Rows[i].Selected = true;
                    break; //If you want to select all rows contain sSearch, skip this line
                }
            }


            //if not found, set current row 0
            if (iFound == false)
            {
                oDataGrid.Rows[0].Selected = true;
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM