简体   繁体   English

如何获取 DataGridView Cell 当前字体和样式

[英]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.在 DataGridView 的 CellFormatting 或 CellPainting 事件处理程序中,我正在设置单元格的字体(加粗)和颜色(前景色和背景色)。

    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. DataGridView控件的CellFormatting事件在请求格式化的方法期间引发,例如在绘制单元格或获取FormattedValue属性时。 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.您更改的CellStyle不会应用于单元格,只会用于格式化值和绘画,因此您无法在CellFormatting事件之外找到这些样式。

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.源代码: DataGridViewCell.GetFormattedValue方法是引发CellFormatting事件的核心方法,如果您查看该方法的源代码,您可以看到您对CellStyle应用的更改并未存储在单元格中。

Solution解决方案

As an option to solve the problem, you can raise the CellFormatting event yourself when you need and use the result of formatting.作为解决问题的一个选项,您可以在需要时自己引发CellFormatting事件并使用格式化的结果。 To do so, you can create such extension method for DataGridViewCell :为此,您可以为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 rowindexcolumnIndex交换,但更改后效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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