2009-07-03 119 views
22

我有一個由多行和多列組成的datagridview。 我想遍歷每一行並檢查特定列的內容。 如果該列包含單詞「否」,我想將整行的前景更改爲紅色。 這是迄今爲止的一些代碼嘗試,但它肯定不工作,開始懷疑如果我需要遍歷每個單元格?C#遍歷DataGridView&更改行顏色

CODE:

foreach (DataGridViewRow dgvr in dataGridView1.Rows) 
     { 
      if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No")) 
      { 
       dgvr.DefaultCellStyle.ForeColor = Color.Red; 
      } 
     } 
+1

什麼是「不工作」?沒有行?細胞無法找到? – Colin 2009-07-03 10:51:31

回答

5
public void ColourChange() 
    { 
     DataGridViewCellStyle RedCellStyle = null; 
     RedCellStyle = new DataGridViewCellStyle(); 
     RedCellStyle.ForeColor = Color.Red; 
     DataGridViewCellStyle GreenCellStyle = null; 
     GreenCellStyle = new DataGridViewCellStyle(); 
     GreenCellStyle.ForeColor = Color.Green; 


     foreach (DataGridViewRow dgvr in dataGridView1.Rows) 
     { 
      if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No")) 
      { 
       dgvr.DefaultCellStyle = RedCellStyle; 
      } 
      if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes")) 
      { 
       dgvr.DefaultCellStyle = GreenCellStyle; 
      } 
     } 
    } 
25

勾起來OnRowDataBound事件,然後做的東西

ASPX(網格):

<asp:.... OnRowDataBound="RowDataBound"..../> 

代碼背後:

protected void RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowIndex == -1) 
     { 
      return; 
     } 

     if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){ 
      e.Row.BackColor=Color.Red; 
     } 
    } 

FOR的WinForms:

hook the **DataBindingComplete** event and do stuff in it: 

    private void dataGridView1_DataBindingComplete(object sender, 
         DataGridViewBindingCompleteEventArgs e) 
    { 
     if (e.ListChangedType != ListChangedType.ItemDeleted) 
     { 
      DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone(); 
      red.BackColor=Color.Red; 

      foreach (DataGridViewRow r in dataGridView1.Rows) 
      { 
       if (r.Cells["FollowedUp"].Value.ToString() 
         .ToUpper().Contains("NO")) 
       { 
        r.DefaultCellStyle = red; 
       } 
      } 
     } 
    } 
+3

對不起,這只是一個直接的WinForms應用程序......... – Goober 2009-07-03 10:42:24

+1

哎呀!一切都如此網絡,我自己深入web項目,因爲3個月以來,每個問題都是關於asp.net – TheVillageIdiot 2009-07-03 10:47:45

+0

似乎很正常,有人被低估了,不知道爲什麼? – TheVillageIdiot 2012-06-29 02:43:47

2

是否有可能有空格或其它字符的單元格的值的一部分?如果是這樣,請嘗試使用Contains方法而不是直接相等。

if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No")) 
0

這是的WinForms解決方案:比鑄造作爲一個字符串,而不是調用toString我真的不看任何

 

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME) 
    { 
     row.DefaultCellStyle.BackColor = Color.LightSalmon; 
     row.DefaultCellStyle.SelectionBackColor = Color.Salmon; 
    } 
} 
 

其他:

private void HighlightRows() 
{ 
    DataGridViewCellStyle GreenStyle = null; 

    if (this.dgridv.DataSource != null) 
    { 
     RedCellStyle = new DataGridViewCellStyle(); 
     RedCellStyle.BackColor = Color.Red; 

     for (Int32 i = 0; i < this.dgridv.Rows.Count; i++) 
     { 
      if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO") 
      { 
       this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle; 
       continue; 
      } 
     } 
    } 
} 
+0

無例外且顏色未更改。我不知道爲什麼請你需要幫助 – 2013-03-27 12:53:52

0

此代碼工作正常,我區別,所以它可能是一個大小寫敏感的錯誤。嘗試使用:

 
dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO" 
9

在您的DataGridView,處理CellFormatting事件:

dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting); 

那麼你的事件處理程序可以是這樣的:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{  
    if(dataGridView1.Columns[e.ColumnIndex].Name == "FollowedUp" && e.Value != null && e.Value.ToString() == "No") 
     dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; 
} 

這樣,你AREN在行上迭代' - 簡單地改變它們在變得可見的時候被繪製/繪製的顏色(並因此重新繪製) quire格式化)在網格中。

0
private void Grd_Cust_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    colorCode == 4 ? Color.Yellow : Color.Brown; 
    if (e.RowIndex < 0 || Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value == DBNull.Value) 
     return; 
    string colorCode = Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value.ToString(); 
    e.CellStyle.BackColor = colorCode == "NO" ? Color.Red : Grd_Cust.DefaultCellStyle.BackColor; 
}