0
我有,其中大部分的数据都包含在一个字典,像这样一类:ASP.Net的GridView:绑定列使用索引,而不是物业
public class Document
{
public string this[string key]
{
get { return (details.ContainsKey(key)) ? details[key] : null; }
}
public long DocumentId { get; set; }
private Dictionary<string, string> details = new Dictionary<string, string>();
public Dictionary<string, string> Details { get { return details; } }
}
我需要将这些对象绑定到一个GridView,我在哪里动态设置要显示的列。
我的问题是,我将如何去建立我的GridView的看向索引拉它的数据(例如,如果我想映射到一个值,在Details
字典中“DocumentName”的键的列):
BoundField bf = new BoundField();
bf.DataField = "DocumentName";
bf.HeaderText = "Document Name";
myGridView.Columns.Add(bf);
这是行不通的,因为 “DocumentName” 不是Document
的属性。我知道如果这些列是硬编码的,我可以在页面上使用<%# Eval("[DocumentName]") %>
来让它看起来索引器,但我该如何通过代码来做到这一点?
或者我不得不创建一个模板列,添加一个标签,并填充每行的创建?
我认为这是我将要去的路线..不是我想要的那么干净,但它必须工作 – John 2012-03-05 21:11:44