有一表记录
单位   月份
a       1
b       2,3
v       3,7
d       5,8
e       3,5,7,12
....
想在stringgrid中实现
   a  b  v  d  e..
1              
2
3             (#)
4
5             (#)
6
7             (#)  
8
....
比如:e单位有3,5,7,12月份,在stringgrid中用“#”分别标识出来,该如何实现!!!

解决方案 »

  1.   

    a  b  v  d  e..
    1              
    2
    3             (#)
    4
    5             (#)
    6
    7             (#)  
    8对这个结构不清楚你的意思.
     SQL语句 case when end应该可以实现的.
      

  2.   

    onDrawCell 事件上件上去判断就可以了~~~然后把符合的加上"#"
      

  3.   

    就是:单位a,b...在stringgrid的cols[0];
          1,2,3,4...月份列在stringgrid的rows[0]。
    好像是个挺简单的问题,就是不知该如何写语句来实想要的功能!!!
    case???怎么写哟??
    我是delphi初学者。
      

  4.   

    begin tran testset nocount on --创建源表
    if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
    gocreate table TestTable(
    FUnit nvarchar(20),
    FMonth nvarchar(50)
    )
    goinsert into TestTable values('a','1')
    insert into TestTable values('b','1,3')
    insert into TestTable values('c','1,2,5,7,9')
    insert into TestTable values('d','1,3,4,5,6,7,8')
    insert into TestTable values('e','1,9,10,11,12')
    insert into TestTable values('f','1,2,3,4,5,9')
    goselect * from TestTable
    go
    --生成目标表
    if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
    gocreate table TempTable(
    FID nvarchar(10)
    )
    goinsert into TempTable values(1)
    insert into TempTable values(2)
    insert into TempTable values(3)
    insert into TempTable values(4)
    insert into TempTable values(5)
    insert into TempTable values(6)
    insert into TempTable values(7)
    insert into TempTable values(8)
    insert into TempTable values(9)
    insert into TempTable values(10)
    insert into TempTable values(11)
    insert into TempTable values(12)
    goselect * from TempTable
    go --循环用游标得到插入数据
    declare @Fmonth nvarchar(50),
            @Funit nvarchar(10)
    declare TestCursor cursor for 
    select * from TestTable
    open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0 
    begin
     --插入相应列并更新数据
      print(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null 
            go 
           update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 
            go ')
      exec(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null 
             go 
             update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 
             go ') fetch next from TestCursor into @Funit, @FMonth
    endclose  TestCursor
    Deallocate TestCursorselect * from TempTablecommit tran Test
      

  5.   

    begin tran testset nocount on --创建源表
    if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
    gocreate table TestTable(
    FUnit nvarchar(20),
    FMonth nvarchar(50)
    )
    goinsert into TestTable values('a','1')
    insert into TestTable values('b','1,3')
    insert into TestTable values('c','1,2,5,7,9')
    insert into TestTable values('d','1,3,4,5,6,7,8')
    insert into TestTable values('e','1,9,10,11,12')
    insert into TestTable values('f','1,2,3,4,5,9')
    goselect * from TestTable
    go
    --生成目标表
    if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
    gocreate table TempTable(
    FID nvarchar(10)
    )
    goinsert into TempTable values(1)
    insert into TempTable values(2)
    insert into TempTable values(3)
    insert into TempTable values(4)
    insert into TempTable values(5)
    insert into TempTable values(6)
    insert into TempTable values(7)
    insert into TempTable values(8)
    insert into TempTable values(9)
    insert into TempTable values(10)
    insert into TempTable values(11)
    insert into TempTable values(12)
    goselect * from TempTable
    go --循环用游标得到插入数据
    declare @Fmonth nvarchar(50),
            @Funit nvarchar(10),
            @SQLstr nvarchar(1000)
    declare TestCursor cursor for 
    select * from TestTable
    open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0   -- + char(13) +'  go '+ char(13)+ char(13) +' go '
    begin
     --插入相应列并更新数据
      exec(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null  ' )
      exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ') fetch next from TestCursor into @Funit, @FMonth
    endclose  TestCursor
    Deallocate TestCursorselect * from TempTablecommit tran test
      

  6.   

    47522341的例子看不太明白,我没有必要去建一个临时表撒,个人认为临时表最好少用,占空间。
    我只是要从表中选出单位所对应的月份,然后在stringgrid中标识一下而已。关键是这(1,2,5,12)这样的情况该如何查找?
      

  7.   

    其实就是这句话有用
    exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ')
    但好像如果查询“1”结果就不对???
      

  8.   

    多费劲呀!遍历一边数据库然后给StringGrid的每个Cell都赋值好啦!
      

  9.   

    gobiz怎么能遍历数据就能找到列中诸如“1”和“2”数据的来呢???
    47522341的方法在找“1”这个数据时把“11”也找了出来,好像是类似于通配符的作用.
    java有将逗号前数据强制转换为数值的indexof,不知delphi有否类似的???
    我都等了三天了,这问题难道都没人能回答???
    急呀!!!
      

  10.   

    首先,月份一共只有12个,所以StringGrid的行数是确定的,即第1行到第12行,其次根据返回记录集的记录数可以得到StringGrid的列数,即记录集的记录指针RecNo就是Cell的列坐标,这样在遍历记录集的时候;
    第一条记录就是需要更新StringGrid中的第一列Cell,那么就分割“1”,转换成整数得到1,即得到了行坐标,那么就可以在Cell[1,1]:='#';
    第二条记录就是需要更新StringGrid中的第二列Cell,那么就分割“2,3”,转换成整数得到2和3,即得到了两个行坐标2和3,那么就可以得到Cell[1,2]:='#'和Cell[1,3]:='#'
    第三条记录……难道很困难吗?
      

  11.   

    说了半天,关键就是不知怎么“分割出1,然后转换成整数”的嘛!
    至于cell这个当然知道!
      

  12.   

    begin tran testset nocount on --创建源表
    if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
    gocreate table TestTable(
    FUnit nvarchar(20),
    FMonth nvarchar(50)
    )
    goinsert into TestTable values('a','01')
    insert into TestTable values('b','01,03')
    insert into TestTable values('c','01,02,05,07,09')
    insert into TestTable values('d','01,03,04,05,06,07,08')
    insert into TestTable values('e','01,09,10,11,12')
    insert into TestTable values('f','01,02,03,04,05,09')
    goselect * from TestTable
    go
    --生成目标表
    if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
    gocreate table TempTable(
    FID nvarchar(10)
    )
    goinsert into TempTable values('01')
    insert into TempTable values('02')
    insert into TempTable values('03')
    insert into TempTable values('04')
    insert into TempTable values('05')
    insert into TempTable values('06')
    insert into TempTable values('07')
    insert into TempTable values('08')
    insert into TempTable values('09')
    insert into TempTable values('10')
    insert into TempTable values('11')
    insert into TempTable values('12')
    goselect * from TempTable
    go --循环用游标得到插入数据
    declare @Fmonth nvarchar(50),
            @Funit nvarchar(10),
            @SQLstr nvarchar(1000)
    declare TestCursor cursor for 
    select * from TestTable
    open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0   -- + char(13) +'  go '+ char(13)+ char(13) +' go '
    begin
     --插入相应列并更新数据
      exec(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null  ' )
      exec(' update TempTable set '+ @Funit+' = ''*'' where charindex(FID,'''+@FMonth+''')> 0 ') fetch next from TestCursor into @Funit, @FMonth
    endclose  TestCursor
    Deallocate TestCursorselect * from TempTablecommit tran test
      

  13.   

    分割字符串函数:ExtractStrings
    转换字符To整型函数:StrToInt、StrToIntDefvar
      s1: String;
      slTmp: TStrings;
    begin
      s1 := '1,3,5';
      slTmp := TStringlist.Create;
      ExtractStrings([','], [], PChar(s1), slTmp);
      //得到结果:slTmp[0] := '1', slTmp[1] := '3', slTmp[2] := '5';
    end;下次要问就问明白一点,免的人家想帮你都帮不到!
      

  14.   

    下面这个是解决过1,2的问题的:begin tran testset nocount on --创建源表
    if exists(select id from sysobjects where id = object_id(N'TestTable')) drop table TestTable
    gocreate table TestTable(
    FUnit nvarchar(20),
    FMonth nvarchar(50)
    )
    goinsert into TestTable values('a','1')
    insert into TestTable values('b','1,3')
    insert into TestTable values('c','1,2,5,7,9')
    insert into TestTable values('d','1,3,4,5,6,7,8')
    insert into TestTable values('e','9,10,11,12')
    insert into TestTable values('f','1,2,3,4,5,9')
    goselect * from TestTable
    go
    --生成目标表
    if exists(select id from sysobjects where id = object_id(N'TempTable')) drop table TempTable
    gocreate table TempTable(
    FID int
    )
    goinsert into TempTable values(1)
    insert into TempTable values(2)
    insert into TempTable values(3)
    insert into TempTable values(4)
    insert into TempTable values(5)
    insert into TempTable values(6)
    insert into TempTable values(7)
    insert into TempTable values(8)
    insert into TempTable values(9)
    insert into TempTable values(10)
    insert into TempTable values(11)
    insert into TempTable values(12)
    goselect * from TempTable
    go --循环用游标得到插入数据
    declare @Fmonth nvarchar(50),
            @Funit nvarchar(10),
            @SQLstr nvarchar(1000)
    declare TestCursor cursor for 
    select * from TestTable
    open TestCursorset nocount offfetch next from TestCursor into @Funit, @FMonthwhile @@Fetch_Status = 0   -- + char(13) +'  go '+ char(13)+ char(13) +' go '
    begin
     --插入相应列并更新数据
      print(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null  ' )
      print(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ')
      
      exec(' alter table TempTable add ' +  @Funit + ' nvarchar(10) null  ' )
      exec(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ') fetch next from TestCursor into @Funit, @FMonth
    endclose  TestCursor
    Deallocate TestCursorselect * from TempTablecommit tran test
      

  15.   

    套用楼主的话说就是:
    -------------
    其实就是这句话有用
    exec(' update TempTable set '+ @Funit+' = ''*'' where FID in ('+@FMonth+') ')
    但好像如果查询“1”结果也对
    -----------------