procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); var R: TRect; org: TPoint; begin if ARow>0 then begin //标题行不能修改 if not SQLResutIsEmpty then begin with Sender as TStringGrid do if (ACol = 2) and (ARow >= FixedRows) then //设备类型 ComboBox begin perform(WM_CANCELMODE, 0, 0); R := CellRect(ACol, ARow); org := Parent.ScreenToClient(ClientToScreen(R.topleft)); with ComboBox9 do begin VisibleFalse; Visible := true; setbounds(org.X, org.Y, R.right - R.left, height); itemindex := Items.IndexOf(Cells[ACol, ARow]); Show; BringTofront; SetFocus; // DroppedDown := true; end; end;
procedure TForm1.VisibleFalse;//悬浮控件先 不可见 begin ComboBox9.Visible := False; Edit10.Visible := False; YearMonthEdit.Visible := False; Edit11.Visible := False; Edit12.Visible := False; ComboBox7.Visible := False; ComboBox8.Visible := False; Edit15.Visible := False; end;
procedure TForm1.ComboBox9Change(Sender: TObject); begin if StringGrid1.Row > 0 then begin CurrUUID := StrToIntDef(StringGrid1.Cells[1, StringGrid1.Row], 0); //µ±Ç°¼Ç¼ÐкŠwith DM.DataModule1.ADOQuery1 do begin CLOSE; SQL.Clear; SQL.Add('UPDATE MainTable set ' + ' [É豸ÀàÐÍ]=' + IntToStr(Integer(ComboBox9.Items.Objects[ComboBox9.ItemIndex])) + '' + ' where [UUID]= ' + IntToStr(CurrUUID) + ' ;' ); ExecSQL; end; ListByUUID; // if StringGrid1.RowCount > 0 then StringGrid1.Row := Form1.StringGrid1.RowCount - 1; //¶¨Î»ÔÚ×î Ð޸ļǼ λÖà // StringGrid1.Cells[0,0]:= IntToStr(StringGrid1.RowCount-1); //0,0ÏÔʾÐÐÊý end; end;
第一列为CheckBox1选择
procedure TForm8.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if stringgrid1.RowCount>2 then begin if ACol>0 then begin if odd(arow) then begin if StringGrid1.Cells[0,ARow]='1' then stringgrid1.Canvas.Brush.Color:=clRed else stringgrid1.Canvas.Brush.Color:=$FFFFC2; stringgrid1.Canvas.FillRect(Rect); end else begin if StringGrid1.Cells[0,ARow]='1' then stringgrid1.Canvas.Brush.Color:=clRed else stringgrid1.Canvas.Brush.Color:=$B8FFF1; stringgrid1.Canvas.FillRect(Rect); end; stringgrid1.Canvas.Font.Color:=clblack; stringgrid1.Canvas.TextOut( rect.Left+2,rect.Top+1,stringgrid1.Cells[ ACol, ARow]); end; end; //标题行 if ARow=0 then begin stringgrid1.Canvas.Brush.Color:=clBlack; stringgrid1.Canvas.FillRect(Rect); stringgrid1.Canvas.Font.Color:=clWhite; stringgrid1.Canvas.Font.Size:=11; stringgrid1.Canvas.TextOut( rect.Left+4,rect.Top+1,stringgrid1.Cells[ ACol, ARow]); end; if acol=0 then //checkbox 点选在第一列 begin with TStringGrid(Sender).Canvas do begin brush.Color:=clWindow; FillRect(Rect); if StringGrid1.Cells[0,ARow]='1' then Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FCheck ) else Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FNoCheck ); end; end; end;
var FCheck,FNoCheck:TBitmap;//全局变量 procedure strGridAddCheckbox ; // var bmp:TBitmap; begin FCheck:= TBitmap.Create; FNoCheck:= TBitmap.Create; bmp:= TBitmap.create; try bmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES )); With FNoCheck Do Begin width := bmp.width div 4; height := bmp.height div 3; canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect ); End; With FCheck Do Begin width := bmp.width div 4; height := bmp.height div 3; canvas.copyrect(canvas.cliprect, bmp.canvas, rect(width,0,2*width,height)); End; finally bmp.free end; end; //在FormCreate里加入
procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); VAR C , R : Longint; var i:integer; aChar:Char; begin if StringGrid1.col=0 then //点选操作 begin if StringGrid1.Cells[0,StringGrid1.row]='1' then StringGrid1.Cells[0,StringGrid1.row]:='0' else StringGrid1.Cells[0,StringGrid1.row]:='1'; StringGrid1.Col:=1; VisibleFalse; end; StringGrid1.MouseToCell(X,Y,C,R); {X,Y由MOUSE事件传入} if (c=0) and (r=0) then begin if StringGrid1.Tag mod 2=0 then aChar:='1' else aChar:='0'; for i:=1 to self.StringGrid1.RowCount-1 do begin self.StringGrid1.Cells[0,i]:=aChar; end; StringGrid1.Tag:= StringGrid1.Tag+1; end; StringGrid1.Repaint; end;
procedure TForm1.DeleBtnClick(Sender: TObject); var i,aCount:integer; tempSQLStr:string; begin aCount:=0; tempSQLStr := ''; for i:=1 to StringGrid1.RowCount -1 do If StringGrid1.Cells[0,i]='1' Then Begin aCount:=aCount+1; tempSQLStr := tempSQLStr + StringGrid1.Cells[1,i]+ ','; //UUID End; if (aCount>0) and (tempSQLStr<>',') and (tempSQLStr<>',,') then begin if Application.MessageBox(pchar('确定要删除 '+ inttostr(aCount)+' 条记录么?'), '警告', MB_OKCANCEL + MB_ICONWARNING) = IDOK then begin tempSQLStr := Copy(tempSQLStr, 1, Length(tempSQLStr) - 1); With DM.DataModule1.ADOQuery1 Do Begin CLOSE; SQL.Clear; SQL.Add('DELETE FROM MainTable WHERE UUID in ( ' + tempSQLStr + ' )'); ExecSQL; End; ListByUUID; end; end; end;