2017-02-15 25 views
1

我有一個dataGridView1,用戶可以輸入信息給它,然後通過單擊按鈕3我希望他搜索任何他在textBox3中鍵入的內容並獲取一個MessageBox,說明該字符串是否在datagridview中找到。如何在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

我給人當我按下一個錯誤BUTTON3! –

+0

爲了迴應你發佈的答案:你必須在做ToString()之前檢查Cells [i] .Value是否爲null。 ToString()不能在空值上工作。 –

回答

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

它總是編譯它不存在於網格中的情況,即使它確實存在! –

相關問題