将NameValueCollection绑定到GridView?

时间:2008-10-20 02:13:45

标签: c# asp.net data-binding namevaluecollection

我应该使用什么样的集合将NameValue集合转换为可绑定到GridView? 直接做的时候没用。

aspx.cs中的代码

  private void BindList(NameValueCollection nvpList)
  {
     resultGV.DataSource = list;
     resultGV.DataBind();
  }

aspx中的代码

<asp:GridView ID="resultGV" runat="server" AutoGenerateColumns="False" Width="100%">
    <Columns>
         <asp:BoundField DataField="Key" HeaderText="Key" />
         <asp:BoundField DataField="Value" HeaderText="Value" />
    </Columns>
</asp:GridView>

任何提示最受欢迎。谢谢。 X

6 个答案:

答案 0 :(得分:8)

你能使用Dictionary&lt; string,string&gt;而不是NameValueCollection。由于字典&lt; T,T&gt;实现IEnumerable你可以使用LINQ:

resultGV.DataSource = from item in nvpDictionary
                      select new { Key = item.Key, Value = item.Value };
resultGV.DataBind();

[编辑]实际上你可以直接使用词典:

resultGV.DataSource = nvpDictionary;
resultGV.DataBind();

如果它没有按您希望的方式映射键/值,您可以随时返回LINQ。 LINQ还允许您将字段重命名为您想要的任何内容。

[编辑]如果您无法更改为使用字典&lt; T,T&gt;,请在方法中将NameValueCollection的副本复制为字典并绑定到它。

private void BindList(NameValueCollection nvpList)
{
   Dictionary<string,string> temp = new Dictionary<string,string>();
   foreach (string key in nvpList)
   {
      temp.Add(key,nvpList[key]);
   }

   resultGV.DataSource = temp;
   resultGV.DataBind();
}

如果你这么做很多,你可以写一个扩展方法来转换成一个字典,并使用它。

public static class NameValueCollectionExtensions
{
   public static Dictionary<string,string> ToDictionary( this NameValueCollection collection )
   {
      Dictionary<string,string> temp = new Dictionary<string,string>();
      foreach (string key in collection)
      {
          temp.Add(key,collection[key]);
      }
      return temp;
   }
}

private void BindList(NameValueCollection nvpList)
{
   resultGV.DataSource = nvpList.ToDictionary();
   resultGV.DataBind();
}

答案 1 :(得分:6)

这有点棘手,因为enumerator只返回键。但是,您可以使用Container.DataItem获取Key值,然后查找NameValueCollection以获取值:

<asp:GridView id="gv" runat="server" AutoGenerateColumns="false">
   <Columns>
      <asp:TemplateField HeaderText="Key">
         <ItemTemplate><%# Container.DataItem %></ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Value">
         <ItemTemplate>
            <%# ((NameValueCollection)gv.DataSource)[(string)Container.DataItem] %>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

答案 2 :(得分:2)

最后,我使用了扩展实现中建议的解决方案,但没有扩展本身。

  private void BindList(NvpList nvpList)
  {
     IDictionary dict = new Dictionary<string, string>();

     foreach (String s in nvpList.AllKeys)
        dict.Add(s, nvpList[s]);

     resultGV.DataSource = dict;
     resultGV.DataBind();
  }

也许做一些静态的帮助类,并在一个地方为我做翻译而不是很多。这个扩展非常方便......: - )

感谢。 X

答案 3 :(得分:1)

如果你有一个嵌套的Repeater(或GridView,我确定),你需要改变Mark Brackett's answer看起来像这样,否则你会得到一个运行 - 无法找到名称为 rpt 的控件的时间错误。

<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
    <li>
        <%# Container.DataItem %>:
        <%# ((NameValueCollection)((Repeater)Container.Parent).DataSource)[(string)Container.DataItem] %>
    </li>    
</ItemTemplate>
</asp:Repeater>

答案 4 :(得分:1)

我发现最好使用StringDictionary进行数据绑定&amp;访问密钥和&amp;值

Dim sDict as New StringDictionary
sDict.Add("1","data1")
sDict.Add("2","data2")
sDict.Add("3","data3")
...

CheckBoxList1.DataSource = sDict
CheckBoxList1.DataValueField = "key"
CheckBoxList1.DataTextField = "value"
CheckBoxList1.DataBind()

答案 5 :(得分:0)

我遇到了类似的问题,涉及将Dictionary(真正的SortedDictionary)绑定到GridView并想要重命名列。最终为我工作的是一些更容易阅读的东西。

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateField HeaderText="Attribute">
      <ItemTemplate>
        <%# ((KeyValuePair<string,string>)Container.DataItem).Key %>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Value">
      <ItemTemplate>
        <%# ((KeyValuePair<string,string>)Container.DataItem).Value %>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

这是通过获取Container.DataItem,GridView尝试显示的当前项目,将其强制转换为实际数据类型(KeyValuePair),然后显示该项目的所需属性。