将 NameValueCollection 绑定到 GridView?
我应该使用哪种集合来将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以使用Dictionary吗? 而不是 NameValueCollection。 由于字典实现 IEnumerable 你可以这样使用 LINQ:
[编辑] 实际上你可以直接使用 Dictionary:
如果它没有按照你想要的方式映射键/值,你可以随时返回 LINQ。 LINQ 还允许您将字段重命名为您想要的任何名称。
[编辑] 如果您无法更改为使用 Dictionary,请在方法中将 NameValueCollection 复制为字典并绑定到它。
如果您经常这样做,您可以编写一个扩展方法来转换为字典,并这样使用它。
Can you use Dictionary<string,string> instead of NameValueCollection. Since Dictionary<T,T> implements IEnumerable you could use LINQ as so:
[EDIT] Actually you may be able to use Dictionary directly as:
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.
If you do this a lot, you could write an extension method to convert to a Dictionary, and use it so.
这有点棘手,因为 枚举器 仅返回键。 但是,您可以使用 Container.DataItem 获取 Key 值,然后查找 NameValueCollection 来获取值:
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:
最后,我使用了扩展实现中建议的解决方案,但没有扩展本身。
也许做一些静态的辅助类,并在一个地方而不是多个地方为我进行翻译。 这个扩展非常方便...:-)
谢谢。 X。
Finally I used solution suggested in your extension implementation, but without extension itself.
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.
如果您有嵌套的
Repeater
(或GridView
,我确定),则需要更改 Mark Brackett 的答案看起来像这样,否则你会得到一个运行时错误,提示无法找到名为的控件rpt。If you have a nested
Repeater
(orGridView
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.我发现最好使用 StringDictionary 进行数据绑定和转换。 访问密钥& 价值
I find it best to use a StringDictionary for databinding & accessing key & value
我遇到了类似的问题,涉及将字典(实际上是 SortedDictionary)绑定到 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.
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.