格式化显示的 SPGridView 值的正确方法是什么?

发布于 2024-08-02 05:07:58 字数 589 浏览 4 评论 0原文

问题

众所周知,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 技术交流群。

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

发布评论

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

评论(5

裂开嘴轻声笑有多痛 2024-08-09 05:07:58

这是我解决这个问题的方法。

<asp:TemplateField HeaderText="Campaign Members">
  <ItemTemplate> 
   <%# RemoveCharacters(Eval("CampaignMembers").ToString())%>
  </ItemTemplate> 
</asp:TemplateField> 

// Make sure declare using System.Text.RegularExpression;
protected string RemoveCharacters(object String) 
{
  string s1 = String.ToString(); 
  string newString = Regex.Replace(s1, @"#[\d-];", string.Empty); 
  newString = Regex.Replace(newString, "#", " "); 
  return newString.ToString();
}

Here is how I solved this issue.

<asp:TemplateField HeaderText="Campaign Members">
  <ItemTemplate> 
   <%# RemoveCharacters(Eval("CampaignMembers").ToString())%>
  </ItemTemplate> 
</asp:TemplateField> 

// Make sure declare using System.Text.RegularExpression;
protected string RemoveCharacters(object String) 
{
  string s1 = String.ToString(); 
  string newString = Regex.Replace(s1, @"#[\d-];", string.Empty); 
  newString = Regex.Replace(newString, "#", " "); 
  return newString.ToString();
}
热鲨 2024-08-09 05:07:58

我通常使用继承自 ITemplate 的 ItemTemplates。在 ItemTemplate 中,我使用 SPFieldxxxValue 类或一些自定义格式化代码。这节省了数据表的循环,并且可以重用项目模板。

ItemTemplates 附加在列绑定中
EG

// Normal Data Binding
SPBoundField fld = new SPBoundField();
fld.HeaderText = field.DisplayName;
fld.DataField = field.InternalName;
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);

// ItemTemplate Binding
TemplateField fld = new TemplateField();
fld.HeaderText = field.DisplayName;
fld.ItemTemplate = new CustomItemTemplateClass(field.InternalName);
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);

ItemTemplate 的示例

public class CustomItemTemplateClass : ITemplate
{
    private string FieldName
    { get; set; }

    public CustomItemTemplateClass(string fieldName, string formatString)
    {
        FieldName = fieldName;
    }

    #region ITemplate Members

    public void InstantiateIn(Control container)
    {
        Literal lit = new Literal();
        lit.DataBinding += new EventHandler(lit_DataBinding);
        container.Controls.Add(lit);
    }
    #endregion

    void lit_DataBinding(object sender, EventArgs e)
    {
        Literal lit = (Literal)sender;
        SPGridViewRow container = (SPGridViewRow)lit.NamingContainer;
        string fieldValue = ((DataRowView)container.DataItem)[FieldName].ToString();

        //Prosses Filed value here
        SPFieldLookupValue lookupValue = new SPFieldLookupValue(fieldValue);

        //Display new value
        lit.Text = lookupValue.LookupValue;
    }
}

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

// Normal Data Binding
SPBoundField fld = new SPBoundField();
fld.HeaderText = field.DisplayName;
fld.DataField = field.InternalName;
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);

// ItemTemplate Binding
TemplateField fld = new TemplateField();
fld.HeaderText = field.DisplayName;
fld.ItemTemplate = new CustomItemTemplateClass(field.InternalName);
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);

An example of a ItemTemplate

public class CustomItemTemplateClass : ITemplate
{
    private string FieldName
    { get; set; }

    public CustomItemTemplateClass(string fieldName, string formatString)
    {
        FieldName = fieldName;
    }

    #region ITemplate Members

    public void InstantiateIn(Control container)
    {
        Literal lit = new Literal();
        lit.DataBinding += new EventHandler(lit_DataBinding);
        container.Controls.Add(lit);
    }
    #endregion

    void lit_DataBinding(object sender, EventArgs e)
    {
        Literal lit = (Literal)sender;
        SPGridViewRow container = (SPGridViewRow)lit.NamingContainer;
        string fieldValue = ((DataRowView)container.DataItem)[FieldName].ToString();

        //Prosses Filed value here
        SPFieldLookupValue lookupValue = new SPFieldLookupValue(fieldValue);

        //Display new value
        lit.Text = lookupValue.LookupValue;
    }
}
幻梦 2024-08-09 05:07:58

这里有几个选项。我不知道所有这些的输出(这将是一篇很好的博客文章),但其中一个应该执行您想要的操作:

如果您想使用原始值,请查看 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.

痴梦一场 2024-08-09 05:07:58

我建议在将值绑定到 spgridview 之前先格式化这些值。首选 Linq 和匿名类型,或者在需要绑定时格式化的字段上调用代码隐藏函数。

DataField='<%# FormatUserField(Eval("UserFieldName")) %>'

或者...也许是一个模板化字段?

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.

DataField='<%# FormatUserField(Eval("UserFieldName")) %>'

or...maybe a templated field?

迷荒 2024-08-09 05:07:58

毕竟,我不知道有任何其他解决方案可以循环遍历 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.

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