将 NameValueCollection 绑定到 GridView?

发布于 2024-07-06 18:41:50 字数 631 浏览 7 评论 0原文

我应该使用哪种集合来将 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。

What kind of collection I should use to convert NameValue collection to be bindable to GridView?
When doing directly it didn't work.

Code in aspx.cs

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

Code in 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>

Any tip most welcome. Thanks. X.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幽梦紫曦~ 2024-07-13 18:41:50

你可以使用Dictionary吗? 而不是 NameValueCollection。 由于字典实现 IEnumerable 你可以这样使用 LINQ:

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

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

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

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

[编辑] 如果您无法更改为使用 Dictionary,请在方法中将 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();
}

Can you use Dictionary<string,string> instead of NameValueCollection. Since Dictionary<T,T> implements IEnumerable you could use LINQ as so:

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

[EDIT] Actually you may be able to use Dictionary directly as:

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

If it doesn't map key/value the way you want you can always go back to LINQ. LINQ would also allow you to rename the fields to whatever you want.

[EDIT] If you can't change to use Dictionary<T,T>, make a copy of the NameValueCollection as a Dictionary in the method and bind to it.

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();
}

If you do this a lot, you could write an extension method to convert to a Dictionary, and use it so.

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();
}
无畏 2024-07-13 18:41:50

这有点棘手,因为 枚举器 仅返回键。 但是,您可以使用 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>

It's a little tricky, because the enumerator returns only the Keys. But, you can get the Key value with Container.DataItem, and then look up into the NameValueCollection to get the value:

<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>
╄→承喏 2024-07-13 18:41:50

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

  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。

Finally I used solution suggested in your extension implementation, but without extension itself.

  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();
  }

maybe do some helper class that will be static and do the translation for me in one place instead of many. This extension is quite handy... :-)

Thanks. X.

感情洁癖 2024-07-13 18:41:50

如果您有嵌套的 Repeater (或 GridView ,我确定),则需要更改 Mark Brackett 的答案看起来像这样,否则你会得到一个运行时错误,提示无法找到名为的控件rpt

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

If you have a nested Repeater (or GridView too, I'm sure), you need to alter Mark Brackett's answer to look like this, or else you'll get a run-time error about not being able to find a control with the name of rpt.

<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
    <li>
        <%# Container.DataItem %>:
        <%# ((NameValueCollection)((Repeater)Container.Parent).DataSource)[(string)Container.DataItem] %>
    </li>    
</ItemTemplate>
</asp:Repeater>
岛徒 2024-07-13 18:41:50

我发现最好使用 StringDictionary 进行数据绑定和转换。 访问密钥& 价值

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()

I find it best to use a StringDictionary for databinding & accessing key & value

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()
终陌 2024-07-13 18:41:50

我遇到了类似的问题,涉及将字典(实际上是 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),然后显示该项目所需的属性。

I had a similar problem involving binding a Dictionary (SortedDictionary really) to a GridView and wanting to rename the columns. What ended up working for me is some thing a little easier to read.

<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>

This works by taking the Container.DataItem, the current item the GridView is trying to display, coercing it into it's actual data type (KeyValuePair) and then displaying the desired property of that item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文