如题所示:在GridView中,当连续3条数据相同时用橙色显示,当连续5条数据相同时,用红色显示;求高手帮忙。

解决方案 »

  1.   

    1、首先确定判断相同记录的原则,比如相同的ID,相同的名称等。2、通过Javascript判断:定义三个变量:
    var currentLine=1;
    var isSame=false;
    var isSameNumber=1;
    var isSameIds={};遍历GridView生成的Table中的行,从第一行开始,
    获取下一行是否相同记录,如果是则isSame=true,isSameNumber=2,
    同时将该行的ID加到isSameIds中,然后当前行currentLine变为2,
    重复上边的步骤。如果相同的等于三,就设置数组isSameIds中的行的颜色为橙色。如果相同的等于四,就设置数组isSameIds中的行的颜色为橙色。如果相同的等于五,就设置数组isSameIds中的行的颜色为红色。其它情况,清空数组isSameIds,设置isSameNumber=1,isSame=false。如果通过C#在cs文件中处理,原理相同,在绑定数据前对获取到的数据进行处理并保存,然后在RowDataBound中根据处理结果设置颜色。
      

  2.   

    统计重复记录的个数,然后个数符合条件的就变色!count
      

  3.   

    很简单 JQUERY +简单判断 从5 开始 向下判断··
      

  4.   

    谢谢各位的帮忙,次问题已得到解决。确实蛮简单的,只是当时没想到而已,现把详细的解答贴出来:
      public void GvRoleInfo_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;     //定义类型
                DataSet ds = red.Get_Red();    //查询的数据源
                int count_index = 0;   //定义一个变量,记载相同数据的条数
                int row_index = 1;    //定义一个行的变量 
                int cells_index = 1;   //定义一个列的变量
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (drv != null)
                    {
                        for (row_index = 0; row_index < ds.Tables[0].Rows.Count - 1; row_index++)    //当查询的数据大于0时,则进行对比判断
                        { 
                            if (ds.Tables[0].Rows[row_index][cells_index].ToString() == ds.Tables[0].Rows[row_index + 1][cells_index].ToString())  //判断同列不同行的数据是否相等
                            {
                                count_index = count_index + 1;
                                if (count_index >= 3 && count_index <= 5)  //连续3列值相同时,则显示为橙色
                                {
                                    e.Row.Cells[cells_index].ForeColor = System.Drawing.Color.Orange;
                                }
                                if (count_index > 5)  //当连续5列值相同时,则显示为红色
                                {
                                    e.Row.Cells[cells_index].ForeColor = System.Drawing.Color.Red;
                                }
                            }
                            else      //当列值不相同时,则count_index 归为 0
                            {
                                count_index = 0;
                            }                    }
                    }
                }
            }
      

  5.   

    上面的这个看着有些乱,下面我给大家贴一个更清楚,明了的解决方法:
     /// <summary>
            /// 绘制DataView的颜色
            /// </summary>
            private void DrawDataView()
            {
                //利用for循环遍历每一行
                for (int i = 0; i < this.GvRoleInfo.Rows.Count; i++)
                {
                    //利用for循环遍历每一列
                    for (int j = 2; j < this.GvRoleInfo.Rows[i].Cells.Count; j++)
                    {
                        //如果此列的值等于0且颜色未被改变过则,改变其字体颜色为红色
                        if (this.GvRoleInfo.Rows[i].Cells[j].Text == "0" && this.GvRoleInfo.Rows[i].Cells[j].ForeColor != System.Drawing.Color.Orange && this.GvRoleInfo.Rows[i].Cells[j].ForeColor != System.Drawing.Color.Red)
                        {
                            this.GvRoleInfo.Rows[i].Cells[j].ForeColor = System.Drawing.Color.Red;
                        }
                        //防止越界异常的跳转
                        if (i > this.GvRoleInfo.Rows.Count - 3)
                        {
                            continue;
                        }
                        //若3行中索引相同的列的值相同,则改变这3行的字体颜色为橙色
                        if (this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 1].Cells[j].Text &&
                            this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 2].Cells[j].Text &&
                            this.GvRoleInfo.Rows[i].Cells[j].ForeColor != System.Drawing.Color.Red)
                        {
                            this.GvRoleInfo.Rows[i].Cells[j].ForeColor = System.Drawing.Color.Orange;
                            this.GvRoleInfo.Rows[i + 1].Cells[j].ForeColor = System.Drawing.Color.Orange;
                            this.GvRoleInfo.Rows[i + 2].Cells[j].ForeColor = System.Drawing.Color.Orange;
                        }
                        //防止越界异常的跳转
                        if (i > this.GvRoleInfo.Rows.Count - 5)
                        {
                            continue;
                        }
                        //若5行中索引相同的列的值相同,则改变这5行的字体颜色为红色
                        if (this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 1].Cells[j].Text &&
                            this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 2].Cells[j].Text &&
                            this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 3].Cells[j].Text &&
                            this.GvRoleInfo.Rows[i].Cells[j].Text == this.GvRoleInfo.Rows[i + 4].Cells[j].Text)
                        {
                            this.GvRoleInfo.Rows[i].Cells[j].ForeColor = System.Drawing.Color.Red;
                            this.GvRoleInfo.Rows[i + 1].Cells[j].ForeColor = System.Drawing.Color.Red;
                            this.GvRoleInfo.Rows[i + 2].Cells[j].ForeColor = System.Drawing.Color.Red;
                            this.GvRoleInfo.Rows[i + 3].Cells[j].ForeColor = System.Drawing.Color.Red;
                            this.GvRoleInfo.Rows[i + 4].Cells[j].ForeColor = System.Drawing.Color.Red;
                        }
                    }
                }
            }