Delphi中StringGrid刪除


開發環境D7
只是部分代碼
StringGrid1事先固定了行數為20,固定了一行為標題行

 

 

----實現單行刪除內容(StringGrid1事先固定了行數為20,固定了一行為標題行),沒有固定多少行的時候請適當改寫,這個是本人測試代碼

 1 var
 2   i:Integer;
 3 begin
 4   if StringGrid1.RowCount<2 then
 5   begin
 6     Exit;
 7   end;
 8   StringGrid1.Rows[StringGrid1.Row].Clear;
 9   if StringGrid1.Row=StringGrid1.RowCount-1 then
10     Exit;
11   for i:=StringGrid1.Row to StringGrid1.RowCount-2 do
12   begin
13     StringGrid1.Rows[i]:=StringGrid1.Rows[i+1];
14     StringGrid1.Rows[i+1].clear;
15   end;
16 
17 end;

--------增加行

StringGrid1.RowCount:=StringGrid1.RowCount+1;

---------減少行

StringGrid1.RowCount:=StringGrid1.RowCount-1;

----------Showhint 單元格內容

1 procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
2   X, Y: Integer);
3 begin
4   if (StringGrid1.MouseCoord(x,y).X>StringGrid1.ColCount-1) or (StringGrid1.MouseCoord(x,y).X<0) then
5     Exit;
6   if (StringGrid1.MouseCoord(x,y).Y>StringGrid1.RowCount-1) or (StringGrid1.MouseCoord(x,y).Y<0) then
7     Exit;
8   StringGrid1.Hint:=StringGrid1.Cells[StringGrid1.MouseCoord(x,y).X,StringGrid1.MouseCoord(x,y).Y];
9 end;

 

 

--------------為單元格賦值    標題行賦值

 1 var
 2   i,j:Integer;
 3 begin
 4    StringGrid1.Cols[0].Text:='A000A';
 5    StringGrid1.Cols[1].Text:='B000B';
 6    for i:=0 to StringGrid1.ColCount-1 do
 7    begin
 8      for j:=0 to StringGrid1.RowCount-1 do
 9      begin
10        if j=0 then
11        begin
12          Continue;
13        end;
14        StringGrid1.Cells[i,j]:=Format('i:=%d ;j:=%d',[i,j]);
15      end;
16    end;
17 end;

 

 

---------------清空StringGrid的內容(標題行不清除)

1 var
2   i:Integer;
3 begin
4    for i:=1 to StringGrid1.RowCount-1 do
5       StringGrid1.Rows[i].Clear;
6 end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM