2016-03-31 112 views
3

是否可以根據多個條件爲datagridview單元格着色? 我知道我可以根據該單元格值更改單元格的顏色。但有可能添加條件,我也可以根據相鄰的單元格值應用顏色。c#基於相鄰單元格值的Datagridview(Winform)單元格着色

要比較單元格的日期和當前日期,我使用下面的代碼。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE") 
    { 
     if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0) 
     { 
      return; 
     } 
     else 
     { 
      if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date) 
      { 
       e.CellStyle.BackColor = Color.Red; 
       e.CellStyle.ForeColor = Color.White; 
      } 
     } 
    } 
    // This section change the color of action proposed description column cell. 
    // i want to change the color in "ACTION PROPOSED DATE"column, if "ACTION PROPOSED DESCRIPTION" contains file closed 
    else if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION") 
    { 
     if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0) 
     { 
      return; 
     } 
     else 
     { 
      string stringvalue = (string) e.Value; 
      stringvalue = stringvalue.ToLower(); 
      if ((stringvalue.IndexOf("file closed") > -1)) 
      { 
       e.CellStyle.BackColor = Color.Purple; 
      } 
     } 
    } 
} 

我想改變顏色的「行動建議DATE」列單元格紫色,如果「行動建議描述」包含「文件關閉」

這是結果我的datagridview

得到

present output

這是結果我期待

expected result

在發佈之前,我搜索了很多,但沒有找到任何答案給我的問題。所以我希望我沒有重複這個問題。

+0

請格式化你的建議的工作代碼 –

回答

2

CellFormatting事件的事件參數表明哪個小區格式化。但是您可以使用其他單元格來確定如何來格式化該單元格。

例如,爲了實現自己的目標,你可以使用這樣的事情:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return; 
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE") 
    { 
     // Take the other column value for the same row 
     var proposedDescription = dg.Rows[e.RowIndex].Cells["ACTION PROPOSED DESCRIPTION"].Value as string; 
     if (proposedDescription != null && proposedDescription.IndexOf("file closed", StringComparison.CurrentCultureIgnoreCase) >= 0) 
     { 
      e.CellStyle.BackColor = Color.Purple; 
      return; 
     } 
     if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0) 
     { 
      return; 
     } 
     else 
     { 
      if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date) 
      { 
       e.CellStyle.BackColor = Color.Red; 
       e.CellStyle.ForeColor = Color.White; 
      } 
     } 
    } 
} 
+0

謝謝,這個作品太:) – princeton

1

您可以簡單地改變這一行代碼

e.CellStyle.BackColor = Color.Purple; 

// Note i did change the column name of ACTION PROPOSE DATE 
// due to syntax property naming rules. 
dataGridView1["ACTION_PROPOSED_DATE", e.RowIndex].Style.BackColor = Color.Purple 

你也必須改變這一點:

else if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION") 

if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION") 

您的程序不會跳過檢查您的專欄建議的描述。

您可以添加,也將檢查相鄰單元格的值是文件關閉這行代碼的下面的條件:

if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date) 
{ 
    e.CellStyle.BackColor = Color.Red; 
    e.CellStyle.ForeColor = Color.White; 
} 

,使當前單元格顏色爲紫色。要做到這一點寫這樣的代碼:

if (dataGridView1["ACTION_PROPOSED_DESCRIPTION", e.RowIndex].Value.ToString() == "File Closed") 
{ 
    dataGridView1["ACTION_PROPOSED_DATE", e.RowIndex].Style.BackColor = Color.Purple; 
} 
+0

。我得到了預期的結果,而不更改列名稱。謝謝你的幫助。但是,如果日期小於當前日期,並且相鄰列是「文件關閉」,則單元格會變爲紅色。它必須是紫色的。你能幫助我嗎?謝謝。 – princeton

+0

你可以在你的else語句中刪除'else'嗎?所以程序會不斷檢查格式化的單元格值 – Roelzkie

+0

嗨,我照你說的那樣做了。仍然沒有變化..我也改變了順序,認爲它會像excel條件格式優先一樣工作。 – princeton

相關問題