datagridview中要实现下面的效果,如何处理?
-
private string[] mColorArray = new string[]
-
{
-
"#ff0000","#9933ff","#0000ff","#9900ff","#ff6600", "#cc3333","#006600",
-
};
-
private void dataGridView1_Sorted(object sender, EventArgs e)
-
{
-
if (dataGridView1.Rows.Count == 0)
-
return;
-
if (dataGridView1.SortedColumn.Index == FIP.Index)
-
{
-
string lastIp = string.Empty;
-
string tmpIp = string.Empty;
-
int colorIndex = 0;
-
for (int iRow = 0; iRow < dataGridView1.Rows.Count; iRow++)
-
{
-
tmpIp = dataGridView1.Rows[iRow].Cells[FIP.Index].Value.ToString();
-
if (iRow + 1 < dataGridView1.Rows.Count)
-
{
-
if (tmpIp.Equals(dataGridView1.Rows[iRow + 1].Cells[FIP.Index].Value.ToString()))
-
{
-
if (!tmpIp.Equals(lastIp))
-
colorIndex++;
-
dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml(mColorArray[colorIndex%7]);
-
dataGridView1.Rows[iRow + 1].DefaultCellStyle.ForeColor = dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor;
-
lastIp = dataGridView1.Rows[iRow + 1].Cells[FIP.Index].Value.ToString();
-
}
-
}
-
}
-
}
-
else
-
{
-
for (int iRow = 0; iRow < dataGridView1.Rows.Count; iRow++)
-
dataGridView1.Rows[iRow].DefaultCellStyle.ForeColor = Color.Black;
-
}
-
}
这样写的结果是,点击单元号列则按单元号列排序,并恢复单元号列的字体颜色;点击IP列,则按IP排序,且按颜色标出相同IP。
阅读(2011) | 评论(0) | 转发(0) |