2012-01-15 29 views
1

DataGridView、列番号、および検索する文字列をフィードでき、検索する列と検索する文字列に基づいてDataGridView行を選択する必要がありますために。私は以下の基本機能を持っています。ちょうどそれを動作させるコードの塊がありません。思考?型チェックした言語でDataGridViewのC#検索列を選択してその行を選択します

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; 
    } 
    } 
} 
+0

ハンガリアン記法は、私の目を焼くことができます。 –

答えて

0
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; 
      } 
     } 
    } 
+0

それはほとんどそれをしました。いくつかの構文はオフになっていましたが、主にそれに答えました。ありがとうございました! –

+0

この回答が本当に役に立ったら、それを受け入れてください。そうでなければ何もしません;-) –

関連する問題