Formatting dataGridView

五迷三道 提交于 2021-02-11 12:35:50

问题


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

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))
            {

            }
        }
    }

and I'm stuck in here.


回答1:


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

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;
    // ...
}

Here is a full sample:

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




回答2:


You can use ListView instead.

Code is very simple.

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



回答3:


You can try my code:

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

In your case:

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

Let me know if that helps.




回答4:


Try this,

EXAMPLE:

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

Source: RichTextBox Cell in a DataGridView



来源:https://stackoverflow.com/questions/32534779/formatting-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!