2009-07-03 99 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; 
}