我正在使用infrastic的超网格,我想显示枚举属性的文本部分。我试着这样做
private void MapToLevel()
{
foreach (var row in HistoryGrid.Rows)
{
row.Cells["LevelId"].Value = row.Cells["LevelId"].Value.ToString();
}
}
这不会改变任何事情。
这种方法在这里被称为
public new void Refresh()
{
LoadData();
HistoryGrid.DataSource = null;
HistoryGrid.DataSource = _BindingSource;
HistoryGrid.DataBind();
MapToLevel();
}
private void LoadData()
{
_histories = _controller.GetAll(_personId, _companyId);
_BindingSource = new BindingSource { DataSource = _histories };
}
这是一个UltraGridColumn扩展,用于转换ValueList 中的枚举
public static ValueList ToValueList(this UltraGridColumn cl, string vlKey, Type t)
{
ValueList vl = new ValueList();
if (vlKey != string.Empty) vl.Key = vlKey;
if (t.IsEnum == true)
{
// Get enum names
string[] names = Enum.GetNames(t);
Array a = Enum.GetValues(t);
int i = 0;
foreach (string s in names)
vl.ValueListItems.Add(a.GetValue(i++), s.Replace("_", " "));
}
cl.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList;
return vl;
}
您在UltraWinGrid的InitializeLayout事件中为正确的列调用它
UltraGridColum cl = e.Layout.Bands[0].Columns["Gender"];
cl.ValueList = cl.ToValueList("gender_list", typeof(GenderEnum));
具有定义为的GenderEnum
public enum GenderEnum
{
Female = 0,
Male = 1
}
当然,我假设您的数据源包含一个列,该列具有枚举的相应值。(在我的例子中,我有一个值为0和1的列"性别")
您需要获得枚举的名称:
row.Cells["LevelId"].Value = Enum.GetName(typeof(YourEnum), row.Cells["LevelId"]);
尝试显式强制转换:
private void MapToLevel()
{
foreach (var row in HistoryGrid.Rows)
{
row.Cells["LevelId"].Value = ((myEnumType)row.Cells["LevelId"].Value).ToString();
}
}