C# 将 NameValueCollection 绑定到 GridView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind NameValueCollection to GridView?
提问by Jaroslav Urban
What kind of collection I should use to convert NameValue collection to be bindable to GridView? When doing directly it didn't work.
我应该使用什么样的集合来将 NameValue 集合转换为可绑定到 GridView?当直接做它没有工作。
Code in aspx.cs
aspx.cs 中的代码
private void BindList(NameValueCollection nvpList)
{
resultGV.DataSource = list;
resultGV.DataBind();
}
Code in aspx
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.
任何提示最受欢迎。谢谢。X。
采纳答案by tvanfosson
Can you use Dictionary<string,string> instead of NameValueCollection. Since Dictionary<T,T> implements IEnumerable you could use LINQ as so:
您可以使用 Dictionary<string,string> 而不是 NameValueCollection。由于 Dictionary<T,T> 实现了 IEnumerable,您可以像这样使用 LINQ:
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:
[编辑] 其实你可以直接使用 Dictionary 作为:
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.
如果它没有按照您想要的方式映射键/值,您可以随时返回 LINQ。LINQ 还允许您将字段重命名为您想要的任何名称。
[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.
[编辑] 如果您不能更改为使用 Dictionary<T,T>,请在方法中复制 NameValueCollection 作为 Dictionary 并绑定到它。
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();
}
回答by Jaroslav Urban
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.
谢谢。X。
回答by Mark Brackett
It's a little tricky, because the enumeratorreturns only the Keys. But, you can get the Key value with Container.DataItem, and then look up into the NameValueCollection to get the value:
这有点棘手,因为枚举器只返回键。但是,您可以使用 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>
回答by Adam Nofsinger
If you have a nested Repeater
(or GridView
too, I'm sure), you need to alter Mark Brackett's answerto 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.
如果您有一个嵌套的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>
回答by fredsmith
I find it best to use a StringDictionary for databinding & accessing key & value
我发现最好使用 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()
回答by PCasagrande
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.
我有一个类似的问题,涉及将字典(实际上是 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>
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.
这是通过获取 Container.DataItem(GridView 试图显示的当前项目),将其强制转换为实际数据类型 (KeyValuePair),然后显示该项目的所需属性来实现的。