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?


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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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