Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

winforms - Using a custom formatter in a DataGridView

So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project.

What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value "0" or "1" or whatever.

One option I considered was to do the whole normalization thing: add a table in the DataSet with the enum names in it, keyed on the enum values, and have my first table hold a reference to this table.

But this is an enum-specific idea. I would like to know if, in general, I can write my own IFormatProvider and ICustomFormatter implementation* for a given type and use that formatter to control how values are displayed in a given column of a DataGridView control (or really in any control, for that matter).

*This is just how I suspect it would be done, if what I'm asking is possible at all. I'm not really dead-set on using these interfaces at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can indeed implement a custom ICustomFormatter, but due to some retardedness on the part of the DataGridView, you need to actually tell it how to apply your formatter.

First set column.DefaultCellStyle.FormatProvider to an instance of your custom formatting class. Then, handle the CellFormatting event:

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
    if (e.CellStyle.FormatProvider is ICustomFormatter) {
        e.Value = (e.CellStyle.FormatProvider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter).Format(e.CellStyle.Format, e.Value, e.CellStyle.FormatProvider);
        e.FormattingApplied = true;
    }
}

The formatter class would look something like this:

public class MyEnumFormatter : IFormatProvider, ICustomFormatter {

    public object GetFormat(Type formatType) {
        if (formatType == typeof(ICustomFormatter))
            return this;
        else
            return null;
    }

    public string Format(string format, object arg, IFormatProvider formatProvider) {
        return ((NameOfEnumType)Convert.ToInt32(arg)).ToString();
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...