2013-10-15 149 views
22

我已將數據從數據庫加載到datagridview,並具有兩列目標值和卷,其中volume>目標值表示卷單元應爲綠色並且卷的目標值爲<目標值,則卷應該位於紅色。我嘗試過但我無法做到。基於條件更改datagridview單元格顏色

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
    if (dataGridView1.Rows.Count > 0 && dataGridView1.Columns.Count > 0) 
    { 
     foreach (DataGridViewRow r in dataGridView1.Rows) 
     { 
      if (Volume > target value) 
      { 
       cell.Style.BackColor = Color.AliceBlue; 
      } 

回答

13

你需要這樣做

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    foreach (DataGridViewRow Myrow in dataGridView1.Rows) 
    {   //Here 2 cell is target value and 1 cell is Volume 
     if (Convert.ToInt32(Myrow .Cells[2].Value)<Convert.ToInt32(Myrow .Cells[1].Value))// Or your condition 
     { 
      Myrow .DefaultCellStyle.BackColor = Color.Red; 
     } 
     else 
     { 
      Myrow .DefaultCellStyle.BackColor = Color.Green; 
     } 
    } 
} 

同時也看看Cell Formatting

+0

謝謝,對不起,這麼晚纔回復它的工作完美.. – preethi

+13

那不是令人難以置信的效率低下?由於每次重繪所有記錄時,都會重繪任何單元格。 如果你正在採用這種方法,整個'foreach'子句不必在CellFormatting處理程序中,但它應該是'dataGridView1_DataBindingComplete'。 – 40Plot

+4

或使用var Myrow = dataGridView1.Rows [e.RowIndex]而不是做循環 –

17

我建議循環每行,每行時間CellFormating被調用,因爲它被稱爲每次都需要刷新單行。

Private Sub dgv_DisplayData_Vertical_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_DisplayData_Vertical.CellFormatting 
     Try 

      If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "6" Then 

       e.CellStyle.BackColor = Color.DimGray 
      End If 
      If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "5" Then 
       e.CellStyle.BackColor = Color.DarkSlateGray 
      End If 
      If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "4" Then 
       e.CellStyle.BackColor = Color.SlateGray 
      End If 
      If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "3" Then 
       e.CellStyle.BackColor = Color.LightGray 
      End If 
      If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "0" Then 
       e.CellStyle.BackColor = Color.White 
      End If 

     Catch ex As Exception 

     End Try 

    End Sub 
+0

最新評論在這裏: 這段代碼會更好Dim value As String = gv_DisplayData_Vertical.Rows(e.RowIndex).Cells(「LevelID」)。Value。 ToString()選擇值大小寫「1」e.CellStyle.BackColor = Color.DimGray退出選擇EndSelect代碼將會更短,更易於閱讀,並且在第三個if語句後選擇大小寫更快。 – Ken

+0

它正在改變每個單元格的顏色 –

14

凱爾和西蒙的答案是CPU資源的大量浪費。 CellFormattingCellPainting事件發生的次數太多,不應用於應用樣式。這裏有兩種更好的方法:

如果你的DataGridView或者至少是決定單元格樣式的列是隻讀的,你應該在RowsAdded事件中更改DefaultCellStyle行。添加新行時,此事件只發生一次。當時應該評估條件,並且應該在其中設置該行的DefaultCellStyle。請注意,該事件也發生在DataBound情況下。

如果您的DataGridView或那些列允許編輯,您應該使用CellEndEditCommitEdit事件來更改DefaultCellStyle

+0

@dotNET我知道帖子是舊的 - 並且是一個很好的觀點。如果你只想改變變化的單個單元而不是整行?假設只有綁定源正在更改,並且單元格編輯事件尚未被調用? – Ken

+0

@Ken:你仍然可以使用'RowsAdded'事件。在這種情況下,您應該使用所需單元格的'Style'屬性,而不是使用該行的'DefaultCellStyle'。如果您需要根據數據源更改更改樣式,則應該聽取數據源本身引發的事件並更改其中的樣式。例如,如果您使用'DataTable'作爲數據源,則可以偵聽'DataTable'的'RowChanged'或'ColumnChanged'事件。 – dotNET

+0

是的,這是正確的和工作 –

1
foreach (DataGridViewRow row in dgvWebData.Rows) 
      { 


       if (Convert.ToString(row.Cells["IssuerName"].Value) != Convert.ToString(row.Cells["SearchTermUsed"].Value)) 
       { 
        row.DefaultCellStyle.BackColor = Color.Yellow; 

       } 
       else 
       { 
        row.DefaultCellStyle.BackColor = Color.White; 
       } 

      } 

,這一完全爲我工作。即使一行被改變,同樣的事件也會照顧到。

-1
//After Done Binding DataGridView Data 
foreach(DataGridViewRow DGVR in DGV_DETAILED_DEF.Rows) 
{ 
    if(DGVR.Index != -1) 
    { 
     if(DGVR.Cells[0].Value.ToString() == "البدلات") 
     { 
      CurrRType = "البدلات"; 
      DataGridViewCellStyle CS = DGVR.DefaultCellStyle; 
      CS.BackColor = Color.FromArgb(0,175,100); 
      CS.ForeColor = Color.FromArgb(0,32,15); 

      CS.Font = new Font("Times New Roman",12,FontStyle.Bold); 
      CS.SelectionBackColor = Color.FromArgb(0,175,100); 
      CS.SelectionForeColor = Color.FromArgb(0,32,15); 
      DataGridViewCellStyle LCS = DGVR.Cells[DGVR.Cells.Count - 1].Style; 
      LCS.BackColor = Color.FromArgb(50,50,50); 
      LCS.SelectionBackColor = Color.FromArgb(50,50,50); 
     } 
     else if(DGVR.Cells[0].Value.ToString() == "الإستقطاعات") 
     { 
      CurrRType = "الإستقطاعات"; 
      DataGridViewCellStyle CS = DGVR.DefaultCellStyle; 
      CS.BackColor = Color.FromArgb(175,0,50); 
      CS.ForeColor = Color.FromArgb(32,0,0); 
      CS.Font = new Font("Times New Roman",12,FontStyle.Bold); 
      CS.SelectionBackColor = Color.FromArgb(175,0,50); 
      CS.SelectionForeColor = Color.FromArgb(32,0,0); 
      DataGridViewCellStyle LCS = DGVR.Cells[DGVR.Cells.Count - 1].Style; 
      LCS.BackColor = Color.FromArgb(50,50,50); 
      LCS.SelectionBackColor = Color.FromArgb(50,50,50); 
     } 
    } 
} 
-1

使簡單

private void dataGridView1_cellformatting(object sender,DataGridViewCellFormattingEventArgs e) 
{ 
    var volume = (int)e.Value; 
    var target = (int)e.Value; 

    // return if rowCount = 0 
    if (this.dataGridView1.Rows.Count == 0) 
     return; 

    if (volume > target) 
     e.CellStyle.BackColor = Color.Green; 
    else 
     e.CellStyle.BackColor = Color.Red; 

} 

看看cell formatting

1

比方說,你必須知道兩件事情上色某些細胞(行不是所有細胞):

  1. 列的名稱或索引。
  2. 將會在單元格內的值。

在你必須使用事件CellFormatting

在我來說,全髖關節置換情況下,我使用這樣

private void DgvTrucksMaster_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    foreach (DataGridViewRow row in dgvTrucksMaster.Rows) 
    { 
     if (Convert.ToInt32(row.Cells["Decade1Hours"].Value) > 0) 
     { 
      row.Cells["Decade1Hours"].Style.BackColor = Color.LightGreen; 
     } 
     else if (Convert.ToInt32(row.Cells["Decade1Hours"].Value) < 0) 
     { 
      // row.DefaultCellStyle.BackColor = Color.LightSalmon; // Use it in order to colorize all cells of the row 

      row.Cells["Decade1Hours"].Style.BackColor = Color.LightSalmon; 
     } 
    } 
} 

,並導致你可以在這裏看到

enter image description here

所以在這裏您可以通過名稱訪問列中某一行的某個單元格行。細胞[「Decade1Hours」]

你怎麼知道這個名字? 那麼在我的情況下,我創建這樣的DataGridView的列。

var Decade1Hours = new DataGridViewTextBoxColumn() 
{ 
    Name = "Decade1Hours", 
    Width = 50, 
    DataPropertyName = "Decade1Hours", 
    ReadOnly = true, 
    DefaultCellStyle = new DataGridViewCellStyle() 
     { 
     Alignment = DataGridViewContentAlignment.MiddleCenter, 
     ForeColor = System.Drawing.Color.Black, 
     Font = new Font(font, FontStyle.Bold), 
     Format = "n2" 
     }, 
    HeaderCell = new DataGridViewColumnHeaderCell() 
     { 
      Style = new DataGridViewCellStyle() 
       { 
       Alignment = DataGridViewContentAlignment.MiddleCenter, 
       BackColor = System.Drawing.Color.Blue 
       } 
     } 
}; 
Decade1Hours.HeaderText = "Дек.1"; 
dgvTrucksMaster.Columns.Add(Decade1Hours); 

嗯...你所需要例如着色部分細胞必須使用細胞指數(從0開始)像## 1 4 5和8排。

和代碼將樂像

private void DgvTrucksMaster_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    foreach (DataGridViewRow row in dgvTrucksMaster.Rows) 
    { 
    if (Convert.ToInt32(row.Cells[1].Value) > 0) 
    { 
     row.Cells[1].Style.BackColor = Color.LightGreen; 
    } 
    } 
} 
相關問題