Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13031416
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2015-03-25 18:07:30

    datagridview中要实现下面的效果,如何处理?

image

 

  1. private string[] mColorArray = new string[]
  2.       {
  3.           "#ff0000","#9933ff","#0000ff","#9900ff","#ff6600", "#cc3333","#006600",
  4.       };
  5.       private void dataGridView1_Sorted(object sender, EventArgs e)
  6.       {
  7.           if (dataGridView1.Rows.Count == 0)
  8.               return;
  9.           if (dataGridView1.SortedColumn.Index == FIP.Index)
  10.           {
  11.               string lastIp = string.Empty;
  12.               string tmpIp = string.Empty;
  13.               int colorIndex = 0;
  14.               for (int iRow = 0; iRow < dataGridView1.Rows.Count; iRow++)
  15.               {
  16.                   tmpIp = dataGridView1.Rows[iRow].Cells[FIP.Index].Value.ToString();
  17.                   if (iRow + 1 < dataGridView1.Rows.Count)
  18.                   {
  19.                       if (tmpIp.Equals(dataGridView1.Rows[iRow + 1].Cells[FIP.Index].Value.ToString()))
  20.                       {
  21.                           if (!tmpIp.Equals(lastIp))
  22.                               colorIndex++;
  23.                           dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml(mColorArray[colorIndex%7]);
  24.                           dataGridView1.Rows[iRow + 1].DefaultCellStyle.ForeColor = dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor;
  25.                           lastIp = dataGridView1.Rows[iRow + 1].Cells[FIP.Index].Value.ToString();
  26.                       }
  27.                   }
  28.               }
  29.           }
  30.           else
  31.           {
  32.               for (int iRow = 0; iRow < dataGridView1.Rows.Count; iRow++)
  33.                   dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor = Color.Black;
  34.           }
  35.       }

    这样写的结果是,点击单元号列则按单元号列排序,并恢复单元号列的字体颜色;点击IP列,则按IP排序,且按颜色标出相同IP。

阅读(2011) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~