格式化显示的 SPGridView 值的正确方法是什么?
问题
众所周知,SharePoint 以纯文本形式将数据保存在数据库中。有些字段甚至具有连接字符串,例如用户字段的
。百分比保存为双精度(1.00000000000000 表示 100%)等。
当然,我想显示列表中显示的数据。
我应该怎么办?
我是否应该使用派生的 SPBoundField 来格式化值(我实际上这样做了,并且它工作正常,直到您想要过滤(可能 SPBoundField 不会格式化我的值,因为我使用 ObjectDataSource 不是列表,并且使用反射器我看到数据源中是否有 SPListItems,然后它格式正确。不是我的情况)
替代文本 http://img199.imageshack.us/ img199/2797/ss20090820110331.png
或者我必须循环遍历所有数据表并相应地格式化每一行?
谢谢
。
Problem
As we know, SharePoint saves data in database in plain text. Some fields even have concatenated strings like <id>;#<value>
for user fields. Percents are saved as doubles (1.00000000000000 for 100%) and etc.
Ofcourse, I want to display data as they are displayed in lists.
What should I do?
Should I use derived SPBoundField to format values (Which I actually did and it works fine until you want to filter (probably SPBoundField won't format me values because i use ObjectDataSource not list and with reflector I saw if there are SPListItems in datasource, then it formats correctly. Not my case)
alt text http://img199.imageshack.us/img199/2797/ss20090820110331.png
Or must I loop through all the DataTable and format each row accordingly?
What are Your techniques?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我解决这个问题的方法。
Here is how I solved this issue.
我通常使用继承自 ITemplate 的 ItemTemplates。在 ItemTemplate 中,我使用 SPFieldxxxValue 类或一些自定义格式化代码。这节省了数据表的循环,并且可以重用项目模板。
ItemTemplates 附加在列绑定中
EG
ItemTemplate 的示例
I normaly use ItemTemplates that inherit from ITemplate. With in the ItemTemplate I use the SPFieldxxxValue classes or some custom formating code. This saves looping through the DataTable and the ItemTemplates can be reused.
The ItemTemplates are attached in Column Binding
E.G
An example of a ItemTemplate
这里有几个选项。我不知道所有这些的输出(这将是一篇很好的博客文章),但其中一个应该执行您想要的操作:
如果您想使用原始值,请查看
SPField*XYZ*Value
类。例如,您提到的表单;#
由 SPFieldUserValue 类表示。您可以将原始文本传递给其构造函数并提取ID,值,最有用的是用户非常容易。Here are a few options. I don't know the output of all of them (would be a good blog post) but one of them should do what you want:
It may also be handy to know that if you ever want to make use of the raw values then have a look at the
SPField*XYZ*Value
classes. For example the form<id>;#<value>
you mention is represented by the class SPFieldUserValue. You can pass the raw text to its constructor and extract the ID, value, and most usefully User very easily.我建议在将值绑定到 spgridview 之前先格式化这些值。首选 Linq 和匿名类型,或者在需要绑定时格式化的字段上调用代码隐藏函数。
或者...也许是一个模板化字段?
I would suggest either to format the values before binding them to the spgridview. Linq and an anonymous type is preffered or to call a code behind function on the field that needs the formatting upon binding.
or...maybe a templated field?
毕竟,我不知道有任何其他解决方案可以循环遍历 DataTable 行并相应地格式化它们。
如果您的 SPGridView 的数据源是列表,请尝试 SPBoundField< /a>.
After all, i did have not know any other solution to loop through DataTable rows and format them accordingly.
If your SPGridView's data source is list, try out SPBoundField.