请教一下,两列数据对比后,将相同结果的行改变颜色。麻烦了

解决方案 »

  1.   

    给你个参考,        /// <summary>
            /// 颜色设置
             /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e != null && e.RowIndex > -1)
                {
                    DataGridViewRow dr = this.dataGridView.Rows[e.RowIndex];
                    int statusCode = Convert.ToInt32(dr.Cells["IntStatusCode"].Value);
                    if (statusCode == 4)
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Gray;
                    }               
                    if (statusCode == 8)
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Gold;
                    }
                    if (statusCode == 12 || statusCode == 16)
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Brown;
                    }               
                    if (statusCode == 20)
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Purple;
                    }
                    if (statusCode == 24 ||statusCode == 28)
                    {
                        dr.DefaultCellStyle.ForeColor = Color.Red;
                    }                
                }
                this.dataGridView.Columns["IntStatusCode"].Visible = false;
            }
      

  2.   

    可以帮忙看看我要怎么写啊??下面代码都不能用.int x=Convert.ToInt32 (DataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value);
    int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value);
    if (x == y)
     {
        DataGridView1.DefaultCellStyle.ForeColor = Color.Red;
     }
      

  3.   

    补充,上面是写在CellFormatting事件里的
      

  4.   

    if (x == y) 
     { 
       foreach(DataGridViewCell cell in DataGridview1.rows[e.RowIndex])
       {
          cell.Style .ForeColor =color.red//这样行不
       }
     }
      

  5.   

    foreach 不行.唉..错误如下:错误 2 “System.Windows.Forms.DataGridViewRow”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Windows.Forms.DataGridViewRow”类型的变量 F:\VS 2005 Projects\KC\KC\WinMain.cs 292 16 KC
      

  6.   

    foreach(DataGridViewCell cell in DataGridview1.rows[e.RowIndex].Cells)
      

  7.   

    还是没有反应,不知道是不是我写错事件了??帮忙看一下〉??private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                int x = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value);
                int y = Convert.ToInt32(DataGridView1.Rows[e.RowIndex].Cells["Safekeep"].Value);
                if (x <= y)
                {
                    foreach (DataGridViewCell cell in DataGridView1.Rows[e.RowIndex].Cells)
                    {
                        DataGridView1.DefaultCellStyle.ForeColor = Color.Red;                }
                }        }
      

  8.   

    汗,我只针对7楼看了下,没看题目
    1.LZ说的是相等,怎么后面就成了x <= y了
    2.符合条件的行变色,哪里用得上foreach再说,DataGridView1.DefaultCellStyle.ForeColor = Color.Red;也和foreach没关系啊 
    if (x <= y)
        DataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; //这句没亲自调试,LZ先试试
      

  9.   

    相等跟<= 差不多吧。后来我想这样比较合理。新人很多都不懂,,
    您写的这句测试没有反应不知道是不是我那边int格式的问题。。
      

  10.   

    private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    {
       string strOne=DataGridView1["Quantity",e.RowIndex].Value.ToString();
       string strTwo=DataGridView1["Safekeep",e.RowIndex].Value.ToString();
       if(strOne==strTwo)
       {
         DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red;
       }}
    //以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK
      

  11.   

    你要的功能貌似和我之前用到的 合并单元格的功能很类似
    http://blog.csdn.net/zerocold_1986/archive/2008/04/14/2291947.aspx
    可以参考下。
      

  12.   

    CellFormatting?
    我做这个功能用的是CellValueChanged
      

  13.   

    15楼的可以哦..不过好像不能转成Int类型去比较。 因为我想变成<=。 这样比较合理。
      

  14.   


    //很简单,把string转换成int就可以了
    private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  

       int One=int.Parse (DataGridView1["Quantity",e.RowIndex].Value.ToString()); 
       int Two=int.Parse (DataGridView1["Safekeep",e.RowIndex].Value.ToString()); 
       if(One<=Two) 
       { 
         DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red; 
       } } 
    //以上在CellFormatting事件里试验一下吧,另外需要注意的是空值null的情况需要单独处理,在此我就不多说了,希望你试验OK 
      

  15.   

    搞定了..之前测试过int.Parse 跟Convert.ToInt32都不行..原来是我数据库的格式问题..谢谢帮助..!!!!!