Grid列内容重复如何操作

west_qiang 2017-12-18 04:46:13



如上图,在StringGrid 控件中如何将已显示的红色字体列的内容变为蓝色字体列的内容,可否点一个按钮就可实现,谢谢各位大神赐教。
...全文
505 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr Dang 2017-12-19
  • 打赏
  • 举报
回复

procedure TForm1.btn1Click(Sender: TObject);
var
    i:Integer;
    slTempList:TStringList;
begin
    slTempList:=TStringList.Create;
    try
        with TempGrid do
        for i:=1 to RowCount-1 do
        begin
            slTempList.Values[Cells[0,i]]:=IntToStr(StrToIntDef(slTempList.Values[Cells[0,i]],0)+1);
            Cells[1,i]:=Cells[0,i]+'-'+RightStr('00'+slTempList.Values[Cells[0,i]],2);
        end;
    finally
        FreeAndNil(slTempList);
    end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
    TempGrid.ColWidths[0]:=100;
    TempGrid.ColWidths[1]:=120;
    TempGrid.ColWidths[2]:=120;
    TempGrid.Cells[0,1]:='35040112';
    TempGrid.Cells[0,2]:='35040112';
    TempGrid.Cells[0,3]:='35040112';
    TempGrid.Cells[0,4]:='35040102';
    TempGrid.Cells[0,5]:='35040102';
    TempGrid.Cells[0,6]:='35040102';
    TempGrid.Cells[0,7]:='35040102';
    TempGrid.Cells[0,8]:='35040105';
    TempGrid.Cells[0,9]:='35040105';
    TempGrid.Cells[0,10]:='35040105';
    TempGrid.Cells[0,11]:='35040105';
    TempGrid.Cells[0,12]:='35040105';
end;

procedure TForm1.btn2Click(Sender: TObject);
var
    i,iPosNo:Integer;
begin
    with TempGrid do
    for i:=1 to RowCount-1 do
    begin
        iPosNo:=Pos('-',Cells[1,i]);
        if iPosNo>0 then
        begin
            Cells[2,i]:=Copy(Cells[1,i],1,iPosNo)+RightStr('00'+IntToStr(StrToInt(Copy(Cells[1,i],iPosNo+1,Length(Cells[1,i])-iPosNo))+1),2);
        end;
    end;
end;
west_qiang 2017-12-19
  • 打赏
  • 举报
回复
引用 5 楼 a295281315 的回复:

procedure TForm1.btn1Click(Sender: TObject);
var
    i:Integer;
    slTempList:TStringList;
begin
    slTempList:=TStringList.Create;
    try
        with TempGrid do
        for i:=1 to RowCount-1 do
        begin
            slTempList.Values[Cells[0,i]]:=IntToStr(StrToIntDef(slTempList.Values[Cells[0,i]],0)+1);
            Cells[1,i]:=Cells[0,i]+'-'+RightStr('00'+slTempList.Values[Cells[0,i]],2);
        end;
    finally
        FreeAndNil(slTempList);
    end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
    TempGrid.ColWidths[0]:=100;
    TempGrid.ColWidths[1]:=120;
    TempGrid.Cells[0,1]:='35040112';
    TempGrid.Cells[0,2]:='35040112';
    TempGrid.Cells[0,3]:='35040112';
    TempGrid.Cells[0,4]:='35040102';
    TempGrid.Cells[0,5]:='35040102';
    TempGrid.Cells[0,6]:='35040102';
    TempGrid.Cells[0,7]:='35040102';
    TempGrid.Cells[0,8]:='35040105';
    TempGrid.Cells[0,9]:='35040105';
    TempGrid.Cells[0,10]:='35040105';
    TempGrid.Cells[0,11]:='35040105';
    TempGrid.Cells[0,12]:='35040105';
end;
这位仁兄的方法得出结果是对的,如果针对结果是 xxxxx-01,xxxxx-02,我想自动累加1,再得到结果变成xxxxx-02,xxxxx-03,要如何处理,谢谢。 分我要怎么给你,第一次发贴?
李龙江 2017-12-18
  • 打赏
  • 举报
回复

//创建窗体,初始化数据
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.RowCount := 16;
  StringGrid1.ColWidths[0] := 15;
  StringGrid1.Cells[1, 0] := '查出的物料号';
  StringGrid1.Cells[2, 0] := '需变成这种';
  StringGrid1.Cells[1, 1] := '35040112';
  StringGrid1.Cells[1, 2] := '35040112';
  StringGrid1.Cells[1, 3] := '35040112';
  StringGrid1.Cells[1, 4] := '35040102';
  StringGrid1.Cells[1, 5] := '35040102';
  StringGrid1.Cells[1, 6] := '35040102';
  StringGrid1.Cells[1, 7] := '35040102';
  StringGrid1.Cells[1, 8] := '35040105';
  StringGrid1.Cells[1, 9] := '35040105';
  StringGrid1.Cells[1, 10] := '35040105';
  StringGrid1.Cells[1, 11] := '35040105';
  StringGrid1.Cells[1, 12] := '35040105';
  StringGrid1.Cells[1, 13] := '35040105';
  StringGrid1.Cells[1, 14] := '35040105';
  StringGrid1.Cells[1, 15] := '35040105';
end;
//点击按钮实现功能
procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;       //定位单元格
  count: Integer;
begin
  for i := 1 to StringGrid1.RowCount do
  begin
    count := 1;
    for j := 1 to i do
    begin
      if StringGrid1.Cells[1, i] = StringGrid1.Cells[1, j] then
      begin
        StringGrid1.Cells[2, i] := StringGrid1.Cells[1, i] + '-' + vartostr(Format('%.2d', [count]));
        Inc(count);
      end
      else
      begin
        count := 1;
      end;
    end;
  end;
end;
Mr Dang 2017-12-18
  • 打赏
  • 举报
回复

procedure TForm1.btn1Click(Sender: TObject);
var
i:Integer;
slTempList:TStringList;
begin
slTempList:=TStringList.Create;
try
with TempGrid do
for i:=1 to RowCount-1 do
begin
slTempList.Values[Cells[0,i]]:=IntToStr(StrToIntDef(slTempList.Values[Cells[0,i]],0)+1);
Cells[1,i]:=Cells[0,i]+'-'+RightStr('00'+slTempList.Values[Cells[0,i]],2);
end;
finally
FreeAndNil(slTempList);
end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
TempGrid.ColWidths[0]:=100;
TempGrid.ColWidths[1]:=120;
TempGrid.Cells[0,1]:='35040112';
TempGrid.Cells[0,2]:='35040112';
TempGrid.Cells[0,3]:='35040112';
TempGrid.Cells[0,4]:='35040102';
TempGrid.Cells[0,5]:='35040102';
TempGrid.Cells[0,6]:='35040102';
TempGrid.Cells[0,7]:='35040102';
TempGrid.Cells[0,8]:='35040105';
TempGrid.Cells[0,9]:='35040105';
TempGrid.Cells[0,10]:='35040105';
TempGrid.Cells[0,11]:='35040105';
TempGrid.Cells[0,12]:='35040105';
end;
west_qiang 2017-12-18
  • 打赏
  • 举报
回复
引用 3 楼 qq49256763 的回复:

SELECT val AS 查出的料号,val +'-'+ right(cast(power(10,2) as varchar)+CAST(ROW_NUMBER() OVER (PARTITION BY val ORDER BY val) AS NVARCHAR),2) AS 需变成的这种 FROM #T
上边的格式化序号有点问题,修复了一下
老兄,我可能没讲清楚,我这个不好用在数据库里解决,我需要用类似以下这种方式,但以下这样方式我写的有问题。 for j:=1 to TempGrid.rowCount do begin bm1:=TempGrid.cells[0,i]; for i:= 1 to TempGrid.RowCount do begin bm2:=TempGrid.Cells[0,i]; if bm1<>bm2 then begin n:=1; TempGrid.Cells[0,i]:=bm2; end else begin bm3:=TempGrid.Cells[0,i]; TempGrid.Cells[0,i]:=bm3+'-'+inttostr(n); n:=n+1; end; end; end;
李龙江 2017-12-18
  • 打赏
  • 举报
回复

SELECT val AS 查出的料号,val +'-'+ right(cast(power(10,2) as varchar)+CAST(ROW_NUMBER() OVER (PARTITION BY val ORDER BY val) AS NVARCHAR),2) AS 需变成的这种 FROM #T
上边的格式化序号有点问题,修复了一下
李龙江 2017-12-18
  • 打赏
  • 举报
回复

CREATE TABLE #T
(
val NVARCHAR(50) NOT NULL
)

INSERT INTO #t
SELECT '35040112' UNION ALL
SELECT '35040112' UNION ALL
SELECT '35040112' UNION ALL
SELECT '35040102' UNION ALL
SELECT '35040102' UNION ALL
SELECT '35040102' UNION ALL
SELECT '35040102' UNION ALL
SELECT '35040105' UNION ALL
SELECT '35040105' UNION ALL
SELECT '35040105' UNION ALL
SELECT '35040105' UNION ALL
SELECT '35040105'


SELECT val AS 查出的料号, val + '-' + REPLICATE('0',1) + CAST(ROW_NUMBER() OVER (PARTITION BY val ORDER BY val) AS NVARCHAR) AS 需变成的这种 FROM #T


qq7118622 2017-12-18
  • 打赏
  • 举报
回复
直接操作表来的更容易些。

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧