简体   繁体   中英

How to get DataGridView Cell current font and style

In a CellFormatting or CellPainting event handler of a DataGridView I'm setting the Font (to be bold) and Color (Fore and Background) of a cell.

    private void DataGrid_CellFormatting(object sender,   DataGridViewCellFormattingEventArgs e)
    {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
        e.CellStyle.ForeColor = Color.White;
        e.CellStyle.BackColor = Color.Black;
    }

    private void DataGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
        e.CellStyle.ForeColor = Color.White;
        e.CellStyle.BackColor = Color.Black;
    }

This works as expected and the desired font and colors are properly displayed. Later I'm trying to read the font and colors from the cells but they appear to be empty.

foreach (DataGridViewRow dgvr in dataGrid.Rows)
{
    Font font = dgvr.Cells[0].Style.Font;
    Color foreColor = dgvr.Cells[0].Style.ForeColor;
    Color backColor = dgvr.Cells[0].Style.BackColor;
}

Font is always null and colors are empty.

Where are they stored and how can I access them?

CellFormatting event of the DataGridView control is raised during methods which request for formatting, like when painting the cell or getting FormattedValue property. The CellStyle which you change will not apply on the cell and just will be used for formatting value and painting, so you can not find those styles outside CellFormatting event.

Source Code: DataGridViewCell.GetFormattedValue method is the central method which cause CellFormatting event be raised and if you take a look at source code of the method, you can see the changes which you apply on CellStyle is not stored in cell.

Solution

As an option to solve the problem, you can raise the CellFormatting event yourself when you need and use the result of formatting. To do so, you can create such extension method for DataGridViewCell :

using System;
using System.Windows.Forms;
using System.Reflection;
public static class DataGridViewColumnExtensions
{
    public static DataGridViewCellStyle GetFormattedStyle(this DataGridViewCell cell) {
        var dgv = cell.DataGridView;
        if (dgv == null)
            return cell.InheritedStyle;
        var e = new DataGridViewCellFormattingEventArgs(cell.RowIndex, cell.ColumnIndex,
            cell.Value, cell.FormattedValueType, cell.InheritedStyle);
        var m = dgv.GetType().GetMethod("OnCellFormatting",
            BindingFlags.Instance | BindingFlags.NonPublic,
            null,
            new Type[] { typeof(DataGridViewCellFormattingEventArgs) },
            null);
        m.Invoke(dgv, new object[] { e });
        return e.CellStyle;
    }
}

Then you can use the method this way:

var s = dataGridView1.Rows[].Cells[0].GetFormattedStyle();
var f = s.Font;
var c = s.BackColor;
var e = new DataGridViewCellFormattingEventArgs(cell.RowIndex, cell.ColumnIndex,
            cell.Value, cell.FormattedValueType, cell.InheritedStyle)

rowindex and columnIndex are swapped, but works great after change

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM