如何将复杂的字典绑定到asp.net中的Gridview?

时间:2010-06-06 19:16:28

标签: c# asp.net data-binding gridview data-dictionary

我已经将 dictionary<string, string> 的早期绑定用于gridview,以显示Urls Text及其HRef作为键值,它就像一个魅力。 但是因为我想用这个替换字典:

dictionary<string, LinkInfo>

绑定出错了!我应该像onItemDataBound那样处理某些事件吗? LinkInfo 结构是:

public struct LinkInfo{
    public string href;
    public bool Enabled;
}

1 个答案:

答案 0 :(得分:1)

是的,你需要加入OnItemDataBound事件。使用模板字段,插入一个ID为“href”的文字

在OnItemDataBound事件中使用

Literal _href = e.Row.FindControl("href") as Literal;  
if(_href != null)
{
  _href = ((LinkInfo)e.Row.DataItem).href;
}

修改:我写了一篇文章,进一步阐述了这个主题:http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns