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 have a SPGridView where I am binding a calculation field. I am dynamically binding data to sp grid view in a web part.

The filter value is displaying like String;#filterexp1, String;#filterexp3, String;#filterexp2 but I need it to display like filterexp1, filterexp2, filterexp3.

Is that possible to format the filter expression?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Yes, it is possible but it is kind of a hack.

Step 1: Make your filter values pretty

For this, you'll need to create a class inheriting from SPGridView and override the GetCallbackResult function:

public class SPFilterGridView : SPGridView
{
   protected override string GetCallbackResult()
   {
      string result = base.GetCallbackResult();

      // do something to alter the result
      // result = ...;

      return result;
   }
}

You'll need to debug to get it right but you should be able to clean up the list here.

Step 2: Fix your FilterExpression

After you make your filter values pretty, you'll notice that the filter won't work anymore. This will be due to your FilterExpression trying to filter on the new "pretty" values instead of the ugly String;#filterexp1 values which are the actual values.

You probably want to override OnPreRender in your web part to dynamically fix your DataSource's FilterExpression:

protected override void OnPreRender(EventArgs e)
{
   base.OnPreRender(e);

   if (!string.IsNullOrEmpty(_gridDS.FilterExpression))
   {
      string fieldName = _grid.FilterFieldName;
      // TODO: whatever logic to reformat FilterExpression (add String;# in there??)
      _gridDS.FilterExpression = string.Format(format, value, fieldName);
   }
}
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.