Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using an ASP.net gridview. The datasource is SPList.Items.GetDataTable().

The gridview is working fine; however, lookup and multilookup columns display as "id;#value". Is there any way to just show the value?

share|improve this question
add comment

3 Answers

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();
}
share|improve this answer
add comment

use SPBoundField in your gridview instead of BoundField

share|improve this answer
    
Okay. I tried SPGridView and spboundfield. I am still seeing the issue with lookupMulti where id;#value is showing when I have multiple values selected in the lookupmulti column. –  Jamil Ferdaus Nov 28 '10 at 7:04
add comment

Alternatively, you could use SPFieldLookupValue (for a single lookup) and SPFieldLookupValueCollection (for a multi-lookup) to parse the field values using SharePoint's built-in methods for processing them.

Here is a revised version of your function for single lookups:

protected string RemoveCharacters(string fieldValue)
{
    SPFieldLookupValue value = new SPFieldLookupValue(fieldValue);
    return value.LookupValue;
}

I've also turned the parameter into a string since your XML passes one in.

Details on processing multiple lookups (and more) can be found in my blog article:

Easily accessing Lookup and User fields

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.