jqGrid display enum as string
I'm attempting to use the ASP.Net MVC version of jqGrid to display a simple data grid. One of the columns in my grid is an Enum and jqGrid is displaying is as an int whereas I want to display it as a string. How can I get jqGrid to display it as a string?
new JQGridColumn { DataField = "ApprovalStatus",
DataType = typeof(ApplicationStatusTypes),
Editable = false,
Width = 200},
public enum ApplicationStatusTypes
{
Unassessed = 0,
AssessmentInProgress = 1,
RequirementsNotMet = 2,
RequirementsPartiallyMet = 3,
RequirementsMet = 4,
Approved = 5
}
When the j开发者_如何学PythonqGrid is rendered the ApprovalStatus column shows up as an int instead of a string. I've tried messing around with DataFormatString on the column but to no avail.
I see that this is an old question, but for any lost soul, that will come here.
First step is to set a SetFormatter(Formatters.Select)
for the column used for displaying an enum.
But then you need to provide a list of enum mappings. jqGrid expects them to be provided as a string in format of enumValue1:enumName1;enumValue2:enumName2 directly to .SetEditOptions(new EditOptions { Value = ... })
- unfortunately the API naming convention is broken here.
The string generation itself is pretty straightforward and can be generalized to the following expression:
string.Join(";", Enum.GetNames(typeof(T)).Zip(Enum.GetValues(typeof(T)).Cast<int>(), (text, val) => val.ToString() + ":" + text));
, where T
is generic parameter being an enum type.
精彩评论