You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否配置SMDBGrid实现不区分大小写的筛选功能?

Fix Case-Insensitive Filtering in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:07:05