2017-02-15 30 views
1

私はdataGridView1を持っていて、ユーザーは情報を入力することができます。次にbutton3をクリックして、彼がtextBox3でタイプしたものを検索したい、そして文字列が見つかったかどうかを示すMessageBoxを取得します。dataGridViewで特定の文字列を検索するにはどうすればよいですか?

これは

private void button3_Click(object sender, EventArgs e) 
    { 
     bool j = false; 
     foreach (DataGridViewRow rows in dataGridView1.Rows) 
     { 

      for (int i = 1; i < rows.Cells.Count; i++) 
      { 
       if(j == false) 
       { 
        if (textBox3.Text == rows.Cells[i].Value.ToString()) 
        { 
         j = true; 
        } 
       } 
       else 
       { 
        break; 
       } 


      } 

     } 



     if (j == true) 
     { 
      MessageBox.Show("It exists!"); 
     } 
     else 
     { 
      MessageBox.Show("It doesn't exist!!"); 
     } 

    } 
+0

チェックこのhttp://stackoverflow.com/questions/13173915/search-for-value-in-datagridview-in-a-column – imsome1

+0

をテストしていませんボタン3! –

+0

答えとして投稿した問題に応じて、ToString()を実行する前にCells [i] .Valueがnullであるかどうかをチェックする必要があります。 ToString()はNULL値では機能しません。 –

答えて

0
bool j = false; 
    foreach (DataGridViewRow rows in dataGridView1.Rows) 
    { 
     for (int i = 1; i < rows.Cells.Count; i++) 
     { 
      if (textBox3.Text == rows.Cells[i].Value.ToString()) 
      { 
       j = true; 
       break; // No need to continue after finding the result 
      } 
     } 
    } 

    if (j) // j is already a boolean 
    { 
     MessageBox.Show("It exists!"); 
    } 
    else 
    { 
     MessageBox.Show("It doesn't exist!!"); 
    } 
0

誰かがあなたに他の同様の答えへのリンクを送ったので、私はとにかく..笑誤ってそこ

に質問に答え、このメソッドがお手伝いします私のコードですテキストが見つかったDataGridViewCellオブジェクトを取得します。私は本当に私が押してエラーを与えるこのコードに

/// <summary> 
    /// Check if a given text exists in the given DataGridView 
    /// </summary> 
    /// <param name="searchText"></param> 
    /// <param name="dataGridView"></param> 
    /// <returns>The cell in which the searchText was found</returns> 
    private DataGridViewCell GetCellWhereTextExistsInGridView(string searchText, DataGridView dataGridView) 
    { 
     DataGridViewCell cellWhereTextIsMet = null; 

     // For every row in the grid (obviously) 
     foreach (DataGridViewRow row in dataGridView.Rows) 
     { 
      foreach (DataGridViewCell cell in row.Cells) 
      { 
       // I did not test this case, but cell.Value is an object, and objects can be null 
       // So check if the cell is null before using .ToString() 

       if (cell.Value != null && searchText == cell.Value.ToString()) 
       { 
        // the searchText is equals to the text in this cell. 
        cellWhereTextIsMet = cell; 
        break; 
       } 
      } 
     } 

     return cellWhereTextIsMet; 
    } 

    private void button_click(object sender, EventArgs e) 
    { 
     DataGridViewCell cell = GetCellWhereTextExistsInGridView(textBox1.Text, myGridView); 
     if (cell != null) 
     { 
      // Value exists in the grid 
      // you can do extra stuff on the cell 
      cell.Style = new DataGridViewCellStyle { ForeColor = Color.Red }; 
     } 
     else 
     { 
      // Value does not exist in the grid 
     } 
    } 
+0

グリッドに存在しないケースをコンパイルします。 –

関連する問題