Question

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.

Was it helpful?

Solution 5

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.

OTHER TIPS

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

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

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.

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top