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

In SpGridview single quote is breaking the filter expression as we are having filter format as "{1} like '{0}'",any idea how we can handle this situation.

share|improve this question

2 Answers 2

You will need to override the OnPreRender method to escape the single quotes:

protected override void OnPreRender(EventArgs e)
{
    if (!string.IsNullOrEmpty(_gridDS.FilterExpression))
    {
        _gridDS.FilterExpression = string.Format(
            _grid.FilteredDataSourcePropertyFormat,
            _grid.FilterFieldValue.Replace("'", "''"),
            _grid.FilterFieldName);
    }
    base.OnPreRender(e);
}

You can check out the full blog post here: SharePoint’s SPGridView, filtering, and apostrophes (full disclosure, this is my blog).

You can also check out a similar question on StackOverflow: SPGridView, data and correct method of ensuring data is safe

share|improve this answer

I suggest using ‘=’ instead of ‘LIKE’ as filtering columns with number values (SPField of type Integer or Number) throws exception. ‘=’ in SQL can be used for text as well as for number comparisons. Give it a try.

share|improve this answer

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.