能否配置SMDBGrid实现不区分大小写的筛选功能?
Hey there! Let's get that SMDBGrid filtering to ignore case—you're already halfway there by digging into the ApplyFilter procedure in SMDBGrid.pas. Here are two solid ways to fix this:
Method 1: Use Dataset's Built-in Case-Insensitive Option
Most Delphi datasets (like TADODataSet, TClientDataSet) support a FilterOptions property that lets you enable case-insensitive filtering with a single line of code.
Update your ApplyFilter procedure like this:
procedure TSMDBGrid.ApplyFilter; var TempCol: Integer; begin if (eoFilterAutoApply in ExOptions) then begin TempCol := LeftCol; BeginUpdate; try if DataLink.Active then begin DataLink.DataSet.CheckBrowseMode; DataLink.DataSet.Filtered := False; // Add this line to enable case-insensitive filtering DataLink.DataSet.FilterOptions := DataLink.DataSet.FilterOptions + [foCaseInsensitive]; DataLink.DataSet.OnFilterRecord := nil; // ... keep the rest of your existing code ...
This works because foCaseInsensitive tells the dataset to ignore case when evaluating the filter condition. Just make sure your dataset supports this property (most standard ones do!).
Method 2: Manually Normalize Case in Filter Strings
If your dataset doesn't support FilterOptions, or if you need more control, modify the filter string to convert both the field value and user input to the same case (all upper or all lower).
Find the part of ApplyFilter where the filter string is built (usually looping through columns to check filter text). For example, if your original code builds a LIKE condition like this:
// Original code (case-sensitive) if FFilterText[Col.Index] <> '' then FilterStr := FilterStr + ' AND (' + Col.FieldName + ' LIKE ' + QuotedStr('%' + FFilterText[Col.Index] + '%') + ')';
Update it to normalize both the field and input to uppercase:
// Modified code (case-insensitive) if FFilterText[Col.Index] <> '' then FilterStr := FilterStr + ' AND (UPPER(' + Col.FieldName + ') LIKE ' + QuotedStr('%' + UpperCase(FFilterText[Col.Index]) + '%') + ')';
For exact matches (using = instead of LIKE), do the same:
// Original exact match FilterStr := FilterStr + ' AND (' + Col.FieldName + ' = ' + QuotedStr(FFilterText[Col.Index]) + ')'; // Modified exact match (case-insensitive) FilterStr := FilterStr + ' AND (UPPER(' + Col.FieldName + ') = ' + QuotedStr(UpperCase(FFilterText[Col.Index])) + ')';
Final Steps
After making either change:
- Recompile the SMDBGrid component package
- Restart your Delphi IDE
- Test the filter with mixed-case input—it should now match regardless of case!
内容的提问来源于stack exchange,提问作者Riyan

