[英]How to bind a custom Enum description to a DataGrid
問題:我有一個枚舉類型,它具有以下樣式的描述標簽:[URL="http://xml.indelv.com/data-binding-enum.html"] 描述標簽教程[/URL]。 我有一個 Windows SQL 服務器數據庫,我從中提取數據(作為整數然后 castine 到枚舉),然后綁定到數據網格。 我不想在枚舉類型中拉出和封裝枚舉類型,而是在枚舉類型中顯示與其關聯的描述標簽。
這是 ASP -
<asp:GridView ID="StatementGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="statementID" OnRowDeleting="StatementGrid_onDeleting" AllowSorting="False">
<Columns>
<asp:BoundField HeaderText="Type" SortExpression="type" DataField="TypeOfStatement" />
<asp:HyperLinkField HeaderText="Statement" DataTextField="StatementText" DataNavigateUrlFormatString="~/Gateway/Statements/View.aspx?statementID={0}" SortExpression="statement" DataNavigateUrlFields="statementID" />
<asp:HyperLinkField DataNavigateUrlFields="statementID" DataNavigateUrlFormatString="~/Gateway/Statements/Update.aspx?statementID={0}" NavigateUrl="~/Gateway/Statements/Update.aspx" HeaderText="Edit" Text="<img src='../../Images/News/news_edit.gif' alt='Edit Statement'/>" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton AlternateText="Delete Statement" ID="DeleteButton" ImageUrl="~/Images/News/news_delete.gif" runat="server" CommandName="Delete" OnClientClick="javascript:return confirm('Are you sure you want to delete this statement?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
There are no statements to display.
</EmptyDataTemplate>
</asp:GridView>
這是綁定的代碼 -
[代碼]
private void BindData()
{
IStatementDao statementDao = DaoFactory.GetStatementDao();
List<Statement> statements;
if (Page.Request.RawUrl.Contains("Gateway"))
{
statements = statementDao.GetAll();
StatementGrid.HeaderStyle.CssClass = "GatewayGridHeader";
StatementGrid.AlternatingRowStyle.CssClass = "GatewayGridAlternatingRow";
}
else
{
// This should never be reached but it keeps the compiler happy!!
statements = statementDao.GetAll();
}
StatementGrid.DataSource = statements;
StatementGrid.DataBind();
DisplayTypeDescriptors();
}
[/代碼]
這是枚舉的 class -
[代碼]
public enum TypeOfStatement
{
[EnumDescription("Dress Code")] DressCode = 1,
[EnumDescription("Lunch Time")] LunchTime = 2,
[EnumDescription("Footwarez")] Footware = 3,
[EnumDescription("achtung")] Warning = 4,
[EnumDescription("Banarna")] Banana = 5,
[EnumDescription("Apfel")] Apple = 6
};c#
[/代碼]
很明顯,一個人可以編寫一個廣泛的方法來做我想做的事,但是有沒有更簡潔的方法?
即時將它們包裝起來,並巧妙地改變您對 SelectedItem (或您正在使用的任何東西)的處理
我的示例使用了已經存在的 Description 屬性。
public class DescriptiveEnum<T> where T: struct
{
private static readonly Dictionary<T,string> descriptions
= new Dictionary<T,string>();
static DescriptiveEnum()
{
foreach (FieldInfo field in
typeof(T).GetFields(BindingFlags.Static
| BindingFlags.GetField | BindingFlags.Public))
{
descriptions.Add((T)field.GetRawConstantValue(),
LookupName(field));
}
}
public readonly T Value;
public DescriptiveEnum(T value)
{
this.Value = value;
}
public override string ToString()
{
string s;
if (!descriptions.TryGetValue(this.Value, out s))
{
// fall back for non declared fields
s = this.Value.ToString();
descriptions[this.Value] = s;
}
return s;
}
private static string LookupName(FieldInfo field)
{
object[] all = field.GetCustomAttributes(
typeof(DescriptionAttribute), false);
if (all.Length == 0)
return field.Name; // fall back
else
return ((DescriptionAttribute)all[0])
.Description; // only one needed
}
public static BindingList<DescriptiveEnum<T>> Make(
IEnumerable<T> source)
{
var list = new BindingList<DescriptiveEnum<T>>();
foreach (var x in source)
list.Add(new DescriptiveEnum<T>(x));
return list;
}
}
示例用法:
public enum Foo
{
[Description("flibble")]
Bar,
[Description("wobble")]
Baz,
// none present, will use the name
Bat
}
Form f = new Form();
f.Controls.Add(new ListBox()
{
Dock = DockStyle.Fill,
DataSource = DescriptiveEnum<Foo>.Make(
new Foo[] { Foo.Bar, Foo.Baz, Foo.Bat }),
});
Application.Run(f);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.