Gridview 将枚举传递给要用作枚举和类型的方法
我试图将枚举传递到一个方法中,该方法将为gridview创建列。我可以将枚举作为Enum passEnum或Type enumType传递,两者都可以工作,只是不能一起传递。我的意思是,如果我将其作为类型传递,Enum.GetNames方法将接受它,如果我将其作为Enum传递,StringEnum.GetString方法将接受它。但我不能传递一个,让他们都接受它,我不能分别传递枚举和类型,让他们都接受它。AMOST工作的方法:Gridview 将枚举传递给要用作枚举和类型的方法,gridview,enums,boundfield,Gridview,Enums,Boundfield,我试图将枚举传递到一个方法中,该方法将为gridview创建列。我可以将枚举作为Enum passEnum或Type enumType传递,两者都可以工作,只是不能一起传递。我的意思是,如果我将其作为类型传递,Enum.GetNames方法将接受它,如果我将其作为Enum传递,StringEnum.GetString方法将接受它。但我不能传递一个,让他们都接受它,我不能分别传递枚举和类型,让他们都接受它。AMOST工作的方法: public static void AddColumnsToGri
public static void AddColumnsToGridView(GridView gv, Enum passEnum, Type enumType)
{
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(enumType))
{
bf = new BoundField();
bf.HeaderText = StringEnum.GetString((passEnum)c);
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gv.Columns.Add(bf);
c++;
}
}
在passEnum下面有一条红色的曲线,上面写着:找不到类型或命名空间“passEnum”。。。等等。出于某种原因,我可以让它在这样的方法之外工作:
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(typeof(PatientRX)))
{
bf = new BoundField();
bf.HeaderText = StringEnum.GetString((PatientRX)c);
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gvRX.Columns.Add(bf);
c++;
}
public static void AddColumnsToGridView<TEnum>(GridView gv)
{
Type enumType = typeof(TEnum);
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(enumType))
{
bf = new BoundField();
bf.HeaderText = StringEnum.GetString((Enum)c);
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gv.Columns.Add(bf);
c++;
}
}
AddColumnsToGridView<MyEnumType>(gridView);
AddColumnsToGridView(gridView, typeof(MyEnum));
StringEnum.GetString方法获取附加到枚举的字符串值。它需要向其传递一个枚举。我怎样才能让它在方法中工作?看起来您正在尝试在那里编写泛型方法,而不是实际使用泛型,请尝试以下操作:
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(typeof(PatientRX)))
{
bf = new BoundField();
bf.HeaderText = StringEnum.GetString((PatientRX)c);
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gvRX.Columns.Add(bf);
c++;
}
public static void AddColumnsToGridView<TEnum>(GridView gv)
{
Type enumType = typeof(TEnum);
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(enumType))
{
bf = new BoundField();
bf.HeaderText = StringEnum.GetString((Enum)c);
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gv.Columns.Add(bf);
c++;
}
}
AddColumnsToGridView<MyEnumType>(gridView);
AddColumnsToGridView(gridView, typeof(MyEnum));
很明显,我没有StringEnum的代码,所以我自己也没有编译过,但我认为应该可以。如果仍然存在问题,请告诉我。我通过为StringEnum类编写一个新方法来解决这个问题,该方法返回枚举的字符串值列表,而不是尝试单独提取每个字符串。。。像这样:
public static void AddColumnsToGridView(GridView gv, Type enumType)
{
gv.Columns.Clear();
List<string> headers = StringEnum.GetStringValueList(enumType);
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(enumType))
{
bf = new BoundField();
bf.HeaderText = headers[c];
bf.DataField = item;
bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
bf.SortExpression = item;
gv.Columns.Add(bf);
c++;
}
}
它调用的方法需要一个Enum,Mike代码中写入的enumType显然不被视为Enum,因为我收到一个错误,无法从TEnum转换为System。Enum,下面是它调用的方法:
public static string GetString(Enum value)
{
string output = null;
Type type = value.GetType();
if (_stringValues.ContainsKey(value))
output = (_stringValues[value] as StringValueAttribute).Value;
else
{
//Look for our 'StringValueAttribute' in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
StringValueAttribute[] attrs = fi.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
if (attrs.Length > 0)
{
_stringValues.Add(value, attrs[0]);
output = attrs[0].Value;
}
}
return output;
}
我没有编写上面的方法或StringEnum类。。。但下面是我添加的用于获取枚举字符串列表的方法:
public static List<string> GetStringValueList(Type enumType)
{
List<string> values = new List<string>();
//Look for our string value associated with fields in this enum
foreach (FieldInfo fi in enumType.GetFields())
{
//Check for our custom attribute
var stringValueAttributes = fi.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
if (stringValueAttributes.Length > 0)
{
values.Add(stringValueAttributes[0].Value);
}
}
return values;
}
如果有人知道像Mike那样使用泛型的方法,我会很感激的。现在这都是一个学习和知识的问题,因为我已经实现了上面提到的解决方案,但我仍然想知道如何以真正通用的方式做到这一点。。。谢谢 感谢泛型课程Mike,它非常有用,但行中仍然存在一个问题:bf.HeaderText=StringEnum.GetStringTEnumc;我得到错误参数1:无法从'TEnum'转换为'System.Enum'抱歉,应该是Enumc而不是TEnumc-编辑的。这种方法可能会遇到的一个问题是,枚举并不总是以0开头,而且它可能会漏掉值,例如,枚举可以定义为:public enum MyEnum{Value1=3,Value2=9}这将与上述代码冲突,因为您假设枚举值将从0开始,一次递增一个。我知道枚举值处于禁用状态,这将是一个问题,当然,但我不打算为此使用具有指定值的枚举,只使用具有从0开始的标准值的枚举列表。