A: 

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?

Johan Leino
I`m wondering if i can use SPField's somehow? They have the logic inside, but i`m wondering if i can somehow let them do the job and just put them on gridview?
Janis Veinbergs
+1  A: 

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.

Alex Angas
I`v tried getter functions, it formats output nicely, however it does not show formatted values in filter dropdown.
Janis Veinbergs
Ah OK. I can't see your image due to a firewall blocking it at my end. In that case you will probably need to 'pre-process' the values. Maybe use the DataBinding event?
Alex Angas
+1  A: 

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 atached 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;
    }
}
JC Vivian
Ugh, tried it, however when filtering, it still displays raw values from database. I`ll probably format datatable.
Janis Veinbergs
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.

Janis Veinbergs