2017-05-03 54 views
0

如何突出顯示基於textbox2中值的datagrid行?如果值匹配文本框值,則基於Hightlight DataGrid行

最終,當在第2列中找到匹配值時,該對應行上的QTY字段(第3列)對於最終用戶掃描的每個QR碼都需要更改-1。一旦QTY值達到0,該行將需要突出顯示爲綠色。

我不能得到它的工作已經嘗試寫的foreach部分,但沒有運氣的幾個不同的方法

我的代碼如下:

private void textBox2_KeyPress(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter) 
     { 
     iCBOMHDataGridView.DataSource = iCBOMHBindingSource; 

      string input = textBox2.Text; 
      string output = ""; 
      textBox2.Text = Regex.Replace(input, @"^\d{4}|[A-z]{2}[0-9]{5},|,|,|\d{|[0-9]{4}/....|\d{1,}\/\d{2,2}\/\d{4}|\s.........|\s|,|,|,|\d*?.$|[*?:/]\n|\r|\r\n", output); 

       foreach (DataGridViewRow row in iCBOMHDataGridView.Rows) 
       { 
       if ((string)row.Cells[2].Value == textBox2.Text) 
        { 
         row.Selected = true; 
        } 
       else 
        { 
         row.Selected = false; 
         MessageBox.Show("Part Number doesn't match"); 
        } 
       } 

     } 
    } 

回答

0

你可以做內部的循環該行的所有單元格並設置單元風格屬性。您可以爲未選擇和選定的行創建不同的樣式,然後根據需要應用這些樣式。

例子:

DataGridViewCellStyle selectedStyle = new DataGridViewCellStyle(); 
selectedStyle.BackColor = Color.LemonChiffon; 
selectedStyle.ForeColor = Color.OrangeRed; 

DataGridViewCellStyle defaultStyle = new DataGridViewCellStyle(); 
defaultStyle.BackColor = System.Drawing.Color.White; 
defaultStyle.ForeColor = Control.DefaultForeColor; 

foreach (DataGridViewRow row in iCBOMHDataGridView.Rows) 
{ 
    if ((string)row.Cells[2].Value == textBox2.Text) 
    { 
     row.Selected = true; 

     if(Decimal.Parse(row.Cells[3].Value.ToString()) > 0) 
      row.Cells[2].Value = Decimal.Parse(row.Cells[2].Value.ToString()) - 1; 
    } 

    if (Decimal.Parse(row.Cells[3].Value.ToString()) <= 0) 
    { 
     foreach (DataGridViewCell col in row.Cells.AsParallel()) 
      col.Style = selectedStyle; 
    } 
    else 
    { 
     foreach (DataGridViewCell col in row.Cells.AsParallel()) 
      col.Style = defaultStyle; 
    } 
} 

如果不通過細胞要循環,你可以簡單地改變每一行的DataRow.DefaultCellStyle財產。但是這會限制您的自定義選項。

+0

最終,當找到匹配值時,對應行上的QTY字段需要由最終用戶掃描的每個QR碼更改-1。一旦QTY值達到0,該行將需要突出顯示爲綠色。 –

+0

然後僅在條件檢查後應用樣式,如果QTY列爲零。 * if((string)row.Cells [cQTY.Name] .Value <= 0)* – ShadowKras

相關問題