使用TDBGrid调用数据中的数据,例如:张三   男    36   编号:0001
李四   男    34   编号:0021
……………………信息比较多,我想当我选择Z的是很可以过滤掉其他的信息,并选择张三的信息,同时可以按动回车的时候调用相应的函数
不知道如何实现呢?

解决方案 »

  1.   

    if key = 'z' then
      过滤相关数据
      

  2.   

    一个根据stringgrid中的数字进行查找的例子procedure Tbafm.StringGrid1KeyPress(Sender: TObject; var Key: Char);
    var
      i:integer;
    begin
      for i:=0 to StringGrid1.RowCount-1 do
        begin
         if Copy(StringGrid1.Cells[0,i],1,1)=Key then
           StringGrid1.Row:=i;
        end;
    end;
      

  3.   

    这个我想你应该先将汉字的首字母保存在数据库,这样更方便点
    然后你用filter或者用sql语句都很容易了
      

  4.   

    数据库先建函数f_ch2py create   function   f_ch2py(@chn   nchar(1))   
      returns   char(1)   
      as   
      begin   
      declare   @n   int   
      declare   @c   char(1)   
      set   @n   =   63   
        
      select   @n   =   @n   +1,   
                    @c   =   case   chn   when   @chn   then   char(@n)   else   @c   end   
      from(   
        select   top   27   *   from   (   
                select   chn   =     
      '吖'   union   all   select   
      '八'   union   all   select   
      '嚓'   union   all   select   
      '咑'   union   all   select   
      '妸'   union   all   select   
      '发'   union   all   select   
      '旮'   union   all   select   
      '铪'   union   all   select   
      '丌'   union   all   select     --because   have   no   'i'   
      '丌'   union   all   select   
      '咔'   union   all   select   
      '垃'   union   all   select   
      '嘸'   union   all   select   
      '拏'   union   all   select   
      '噢'   union   all   select   
      '妑'   union   all   select   
      '七'   union   all   select   
      '呥'   union   all   select   
      '仨'   union   all   select   
      '他'   union   all   select   
      '屲'   union   all   select     --no   'u'   
      '屲'   union   all   select     --no   'v'   
      '屲'   union   all   select   
      '夕'   union   all   select   
      '丫'   union   all   select   
      '帀'   union   all   select   @chn)   as   a   
      order   by   chn   COLLATE   Chinese_PRC_CI_AS     
      )   as   b   
      return(@c)   
      end   
        
      go   
    调用,假设你的表为表1,张三列为姓名在form的keydown事件里
    if key = 13 then
    begin
      adoquery1.active:= false;
      adoquery1.sql.text:= 'select * from 表1 where dbo.f_ch2py(substring(姓名,1,1)) = '''+
                           edit1.text+'''';    //edit1.text的值为 Z
      adoquery1.active:= true;
      

  5.   

    数据库如果是AcceSS的也可以实现吗?
      

  6.   

    多插一个列,名为[显],设类型为[是/否]在form的keydown事件里procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);Function GetPY(chnchar:string):char;
    begin
      case Word(chnchar[1]) shl 8 +word(chnchar[2]) of
        $B0A1..$B0C4:  result:='A';
        $B0C5..$B2C0:  result:='B';
        $B2C1..$B4ED:  result:='C';
        $B4EE..$B6E9:  result:='D';
        $B6EA..$B7A1:  result:='E';
        $B7A2..$B8C0:  result:='F';
        $B8C1..$B9FD:  result:='G';
        $B9FE..$BBF6:  result:='H';
        $BBF7..$BFA5:  result:='J';
        $BFA6..$C0AB:  result:='K';
        $C0AC..$C2E7:  result:='L';
        $C2E8..$C4C2:  result:='M';
        $C4C3..$C5B5:  result:='N';
        $C5B6..$C5BD:  result:='O';
        $C5BE..$C6D9:  result:='P';
        $C6DA..$C8BA:  result:='Q';
        $C8BB..$C8F5:  result:='R';
        $C8F6..$CBF9:  result:='S';
        $CBFA..$CDD9:  result:='T';
        $CDDA..$CEF3:  result:='W';
        $CEF4..$D1B8:  result:='X';
        $D1B9..$D4D0:  result:='Y';
        $D4D1..$D7F9:  result:='Z';
      else
        result:=char(0);
      end;
    end;begin
    if key = 13 then
    begin
      adoquery2.active:= false;
      adoquery2.sql.text:= 'update 表1 set [显] = 0';  // access里我不太清楚真假的值现假设为0
      adoquery2.execsql;
      while not adoquery1.eof do
      begin
        if GetPY(copy(adoquery1.fieldbyname('姓名').asstring,1,2)) = 'Z' then
        begin
          adoquery2.sql.text:= 'update 表1 set [显] = 1 where [姓名] = '''+ adoquery1.fieldbyname('姓名').asstring+'''';
          adoquery2.execsql;
          adoquery1.next;
        end;
      end;  adoquery1.active:= false;
      adoquery1.sql.text:= 'select * from 表1 where [显] = '''+
                           edit1.text+'''';    //edit1.text的值为 Z
      adoquery1.active:= true;
    end;手写,你测一下
      

  7.   

    兄弟,key = 13 是什么意思呢?
      

  8.   

    key =13 是回车吖对了,那里最后几句改成  
      adoquery1.active:= false;
      adoquery1.sql.text:= 'select * from 表1 where [显] = 1';
      adoquery1.active:= true;