在DataGridView中使用自定义格式化程序 [英] Using a custom formatter in a DataGridView
问题描述
DataTable
,列有一个包含 int
值的列;这些值实际上意味着代表我在项目中的一些枚举
类型。 喜欢做的是将一个 DataGridView
绑定到此表,并且列显示的名称为枚举
,而不是整数值0或1或任何其他。
我认为的一个选项是做整个规范化的事情:在 DataSet
与枚举
中的名称,键入枚举
值,并让我
但是这是一个枚举
- 具体想法我想知道,一般来说,我可以为给定的类型编写自己的 IFormatProvider
和 ICustomFormatter
implementation *并且使用该格式化器来控制如何在 DataGridView
控件(或者真正在任何控件)的给定列中显示值。
*这只是我怀疑它会做的,如果我问的是可能的话。我根本就不会使用这些接口。
ICustomFormatter
,但由于 DataGridView
的某些延迟,您需要实际告诉您如何应用格式化程序。
首先将 column.DefaultCellStyle.FormatProvider
设置为自定义格式化类的一个实例。然后,处理 CellFormatting
事件:
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;
}
}
格式化程序类将如下所示: p>
public class MyEnumFormatter:IFormatProvider,ICustomFormatter {
public object GetFormat(Type formatType){
if(formatType == typeof(ICustomFormatter))
return this;
else
返回null;
}
公共字符串格式(字符串格式,对象arg,IFormatProvider formatProvider){
return((NameOfEnumType)Convert.ToInt32(arg))ToString();
}
}
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.
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();
}
}
这篇关于在DataGridView中使用自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!