格式化dataGridView

时间:2015-09-12 03:07:42

标签: c# datagridview formatting

我在dataGridView中有这样的数据:

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

我的问题是当codetrans和datetrans相同时如何更改forecolor,但codetrans和datetrans index不会改变?

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

            }
        }
    }

我被困在这里。

4 个答案:

答案 0 :(得分:1)

目前还不是很清楚你想要实现的目标。假设您要指示重复的行。我们首先定义一些帮手:

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事件中使用它们:

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);
        }
    }
}

Result

答案 1 :(得分:0)

您可以改用ListView。

代码非常简单。

yourListView.Items[1].ForeColor = Color.Red;

答案 2 :(得分:0)

您可以尝试我的代码:

yourDataGirdview.Rows[0].DefaultCellStyle.ForeColor = Color.White;
yourDataGirdview.Rows[0].DefaultCellStyle.BackColor = Color.White;
yourDataGirdview.Rows[0].Cells[1].Style.Font = new Font(this.Font, FontStyle.Bold);

在你的情况下:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.DataGridView.Equals(row.Cells[0].Value))
        {
            row.DefaultCellStyle.ForeColor = Color.White;
            row.DefaultCellStyle.BackColor = Color.White;
            row.Cells[1].Style.Font = new Font(this.Font, FontStyle.Bold);
        }
    }
}

如果有帮助,请告诉我。

答案 3 :(得分:0)

试试这个,

实施例

using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace DataGridViewRichTextBox
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            LoadDatGridView();
        }

        private readonly DataTable _table = new DataTable();
        private void LoadDatGridView()
        {
            _table.Columns.Add("Column1");
            _table.Rows.Add("CDTRS1 2015/9/14 BR01");
            _table.Rows.Add("CDTRS2 2015/9/15 BR02");
            _table.Rows.Add("CDTRS2 2015/9/15 BR03");
            _table.AcceptChanges();

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = _table;

            foreach (DataRow row in _table.Rows)
            {
                var splits = row[0].ToString().Split(' ');
                var value = string.Format("{0} {1}", splits[0], splits[1]);
                using (var rich = new RichTextBox{Text = row[0].ToString()})
                {
                    if (IsValueExist(value))
                    {
                        rich.Text = row[0].ToString();
                        rich.Select(0, value.Length);
                        rich.SelectionColor = Color.Brown;
                        row[0] = rich.Rtf;
                    }
                    else
                    {
                        rich.Text = row[0].ToString();
                        rich.SelectAll();
                        rich.SelectionColor = Color.Black;
                        row[0] = rich.Rtf;
                    }
                }
            }
            _table.AcceptChanges();
        }

        private bool IsValueExist(string value)
        {
            return dataGridView1.Rows.Cast<DataGridViewRow>().Count(row => row.Cells[0].Value.ToString().Contains(value)) > 1;
        }
    }
}

enter image description here

来源:RichTextBox Cell in a DataGridView