格式化dataGridView [英] Formatting dataGridView

查看:73
本文介绍了格式化dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在dataGridView中有一个像这样的数据:

I have a data in dataGridView like this:

codetrans | datetrans | codeitem
----------+-----------+----------
 CDTRS1   | 2015/9/14 |  BR01
 CDTRS2   | 2015/9/15 |  BR02
 CDTRS2   | 2015/9/15 |  BR03

我的问题是,当codetrans和datetrans相同时如何更改原色,但codetrans和datetrans索引不变?

My question is how to change forecolor when codetrans and datetrans is the same, but codetrans and datetrans index won't change?

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.DataGridView.Equals(row.Cells[0].Value))
            {

            }
        }
    }

我被困在这里.

推荐答案

目前尚不清楚您要实现什么目标.假设您要指示重复的行.首先定义一些帮助器:

It's not very clear what are you trying to achieve. Assuming you want to indicate a duplicate row. Let first define some helpers:

static bool EqualKeys(DataGridViewRow x, DataGridViewRow y, string[] keyNames)
{
    foreach (var keyName in keyNames)
        if (!Equals(x.Cells[keyName].Value, y.Cells[keyName].Value)) return false;
    return true;
}
static bool IsDuplicateRow(DataGridView dg, int rowIndex, string[] keyNames)
{
    var row = dg.Rows[rowIndex];
    for (int i = rowIndex - 1; i >= 0; i--)
        if (EqualKeys(row, dg.Rows[i], keyNames)) return true;
    return false;
}
static bool IsDuplicateRowCell(DataGridView dg, int rowIndex, int columnIndex, string[] keyNames)
{
    return keyNames.Contains(dg.Columns[columnIndex].Name) &&
        IsDuplicateRow(dg, rowIndex, keyNames);
}

现在,您可以通过 CellFormatting 事件使用它们:

Now you can use them from your CellFormatting event:

if (IsDuplicateRowCell((DataGridView)sender, e.RowIndex, e.ColumnIndex, keyNames))
{
    // Do whatever you like with the cell style 
    e.CellStyle.ForeColor = Color.Red;
    // ...
}

以下是完整的示例:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Samples
{
    static class Test
    {
        static bool EqualKeys(DataGridViewRow x, DataGridViewRow y, string[] keyNames)
        {
            foreach (var keyName in keyNames)
                if (!Equals(x.Cells[keyName].Value, y.Cells[keyName].Value)) return false;
            return true;
        }
        static bool IsDuplicateRow(DataGridView dg, int rowIndex, string[] keyNames)
        {
            var row = dg.Rows[rowIndex];
            for (int i = rowIndex - 1; i >= 0; i--)
                if (EqualKeys(row, dg.Rows[i], keyNames)) return true;
            return false;
        }
        static bool IsDuplicateRowCell(DataGridView dg, int rowIndex, int columnIndex, string[] keyNames)
        {
            return keyNames.Contains(dg.Columns[columnIndex].Name) && IsDuplicateRow(dg, rowIndex, keyNames);
        }
        class Data
        {
            public string codetrans { get; set; }
            public string datetrans { get; set; }
            public string codeitem { get; set; }
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var dataSet = new List<Data>
            {
                new Data { codetrans="CDTRS1", datetrans ="2015/9/14", codeitem="BR01" },
                new Data { codetrans="CDTRS2", datetrans ="2015/9/15", codeitem="BR02" },
                new Data { codetrans="CDTRS2", datetrans ="2015/9/15", codeitem="BR03" },
                new Data { codetrans="CDTRS2", datetrans ="2015/9/15", codeitem="BR03" },
                new Data { codetrans="CDTRS2", datetrans ="2015/9/15", codeitem="BR05" },
            };
            var form = new Form();
            var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form };
            var keyNames = new[] { "codetrans", "datetrans", "codeitem" };
            dg.CellFormatting += (sender, e) =>
            {
                if (IsDuplicateRowCell((DataGridView)sender, e.RowIndex, e.ColumnIndex, keyNames))
                    e.CellStyle.ForeColor = Color.Red;
            };
            dg.CellValueChanged += (sender, e) => ((DataGridView)sender).Invalidate();
            dg.DataSource = dataSet;
            Application.Run(form);
        }
    }
}

这篇关于格式化dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆