2012-12-25 86 views
2

我必須編寫一個DataGridView並以編程方式填充它。 我想要當用戶搜索一個單詞例如鍵,並且有包含它的單元格(例如它的關鍵字)只是在關鍵字中突出顯示搜索到的單詞鍵而不是all關鍵字或不是單元格或行。 這裏是填充DataGridView代碼:在datagridview c中更改搜索關鍵字的顏色#

 private void Fill() 
    { 
     try 
     { 
      if (dataGridView1 != null) 
      { 
       dataGridView1.ColumnCount = 11; 
       dataGridView1.Columns[0].HeaderText = Resources.Form1_Fill_ID; 
       dataGridView1.Columns[1].HeaderText = Resources.Form1_Fill_Family; 
       dataGridView1.Columns[2].HeaderText = Resources.Form1_Fill_Cellphone; 
       dataGridView1.Columns[3].HeaderText = Resources.Form1_Fill_Phone1; 
       dataGridView1.Columns[4].HeaderText = Resources.Form1_Fill_Phone2; 
       dataGridView1.Columns[5].HeaderText = Resources.Form1_Fill_Phone3; 
       dataGridView1.Columns[6].HeaderText = Resources.Form1_Fill_Fax; 
       dataGridView1.Columns[7].HeaderText = Resources.Form1_Fill_CompanyName; 
       dataGridView1.Columns[8].HeaderText = Resources.Form1_Fill_Agency; 
       dataGridView1.Columns[9].HeaderText = Resources.Form1_Fill_Brands; 
       dataGridView1.Columns[10].HeaderText = Resources.Form1_Fill_Address; 

       dataGridView1.Columns[0].Name = Resources.Form1_Fill_ID; 
       dataGridView1.Columns[1].Name = Resources.Form1_Fill_Family; 
       dataGridView1.Columns[2].Name = Resources.Form1_Fill_Cellphone; 
       dataGridView1.Columns[3].Name = Resources.Form1_Fill_Phone1; 
       dataGridView1.Columns[4].Name = Resources.Form1_Fill_Phone2; 
       dataGridView1.Columns[5].Name = Resources.Form1_Fill_Phone3; 
       dataGridView1.Columns[6].Name = Resources.Form1_Fill_Fax; 
       dataGridView1.Columns[7].Name = Resources.Form1_Fill_CompanyName; 
       dataGridView1.Columns[8].Name = Resources.Form1_Fill_Agency; 
       dataGridView1.Columns[9].Name = Resources.Form1_Fill_Brands; 
       dataGridView1.Columns[10].Name = Resources.Form1_Fill_Address; 
      } 

      _conn.ConnectionString = _connectionString; 
      var cmd = new OleDbCommand("Select * from contacts ", _conn); 
      _conn.Open(); 
      OleDbDataReader reader = cmd.ExecuteReader(); 

      int i = 0; 
      while (reader != null && reader.Read()) 
      { 
       if (dataGridView1 != null) 
       { 
        dataGridView1.Rows.Add(1); 
       } 
       if (dataGridView1 != null) 
       { 
        var row = dataGridView1.Rows[i]; 

        row.Cells[Resources.Form1_Fill_ID].Value = reader[0].ToString(); 
        row.Cells[Resources.Form1_Fill_Family].Value = reader[1].ToString(); 
        row.Cells[Resources.Form1_Fill_Cellphone].Value = reader[2].ToString(); 
        row.Cells[Resources.Form1_Fill_Phone1].Value = reader[3].ToString(); 
        row.Cells[Resources.Form1_Fill_Phone2].Value = reader[4].ToString(); 
        row.Cells[Resources.Form1_Fill_Phone3].Value = reader[5].ToString(); 
        row.Cells[Resources.Form1_Fill_Fax].Value = reader[6].ToString(); 
        row.Cells[Resources.Form1_Fill_CompanyName].Value = reader[7].ToString(); 
        row.Cells[Resources.Form1_Fill_Agency].Value = reader[8].ToString(); 
        row.Cells[Resources.Form1_Fill_Brands].Value = reader[9].ToString(); 
        row.Cells[Resources.Form1_Fill_Address].Value = reader[10].ToString(); 

       } 
       i++; 
      } 

     } 
     catch (Exception ex) 
     { 
      return; 
     } 
     finally 
     { 
      _conn.Close(); 
     } 
    } 

這裏是檢索碼

 private void searchBtn_Click(object sender, EventArgs e) 
    { 
     // Code to search the alphanumneric Part Number (in Column1 header called "PART NUMBER") and highlihgt the row 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 

      try 
      { 

       if (row.Cells[0].Value.ToString().Contains(searchTxt.Text)) 
        //row.Cells[0].Style.ForeColor = Color.Red; 
       { 
        var t = row.Cells[0].Value.ToString(); 

       } 
       if (row.Cells[1].Value.ToString().Contains(searchTxt.Text)) 
         row.Cells[1].Style.ForeColor = Color.Red; 
       if (row.Cells[2].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[2].Style.ForeColor = Color.Red; 
       if (row.Cells[3].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[3].Style.ForeColor = Color.Red; 
       if (row.Cells[4].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[4].Style.ForeColor = Color.Red; 
       if (row.Cells[5].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[5].Style.ForeColor = Color.Red; 
       if (row.Cells[6].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[6].Style.ForeColor = Color.Red; 
       if (row.Cells[7].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[7].Style.ForeColor = Color.Red; 
       if (row.Cells[8].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[8].Style.ForeColor = Color.Red; 
       if (row.Cells[9].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[9].Style.ForeColor = Color.Red; 
       if (row.Cells[10].Value.ToString().Contains(searchTxt.Text)) 
        row.Cells[10].Style.ForeColor = Color.Red; 
      } 
      catch (Exception) 
      { 
       return; 
      } 
     } 
    } 
+0

你可以在你dgv.see使用'richtextbox'此鏈接:http://stackoverflow.com/questions/10233948/highlighting-a-word- in-a-datagridview – Arash

回答

0

如果搜索只依靠客戶端加載數據,那麼你爲什麼不建議客戶使用瀏覽器搜索功能。

如果仍然需要爲客戶提供此功能,那麼爲什麼您要在服務器端代碼執行此任務....您可以在JavaScript中處理此問題。 以下是關於如何訪問javascript中的datagridview的示例鏈接。

  1. http://www.netomatix.com/development/gridviewclientsideaccess.aspx

  2. http://www.thescarms.com/dotnet/webdatagrid.aspx

+0

它是Windows應用程序 – Morteza

0

這是不可能與標準.NET控件。 您可以嘗試Telerik winform suite或類似的,它允許使用HTML格式來突出顯示文本的一部分。

您真的需要DataGridView嗎?如果您只用它來顯示數據,您也可以嘗試使用WebBrowser控制並在那裏繪製一個html表格。

你也應該重構你的搜索代碼:

for(int i=0; i<11; i++) 
{ 
    if (row.Cells[i].Value.ToString().Contains(searchTxt.Text)) 
    row.Cells[i].Style.ForeColor = Color.Red; 
} 
+0

用戶想要簡單的exe文件。 在應用程序中有沒有什麼可以用來代替Datagridview? – Morteza

+0

謝謝For循環代碼 – Morteza

+0

@Morteza正如我所說,如果沒有必要從表中選擇一些東西,請使用webbrowser控件繪製表格 – VladL