MyCol := TcxColumn.Create; ... MyCol.PropertiesClass := TcxImageProperties; ImageProps := TcxImageProperties(MyCol.Properties); ImageProps.Center := True; ImageProps.GraphicClassName := ''; ImageProps.OnGetGraphicClass := GetThumbnailGraphicClass; ImageProps.Stretch := True; ... Procedure GetThumbnailGraphicClass: procedure TCORSA.GetThumbnailGraphicClass(AItem: TObject; ARecordIndex: Integer; APastingFromClipboard: Boolean; var AGraphicClass: TGraphicClass); begin if AnsiSAmeText(FThumbNailExtension, '.TIF') then AGraphicClass := TGraphicClass(GetClass('TTiffGraphic')) else if AnsiSAmeText(FThumbNailExtension, '.JPG') then AGraphicClass := TGraphicClass(GetClass('TJPEGImage')) end; The actual thumbnail data is loaded into the grid via streams: MStream := TMemoryStream.Create; Stream := TStringStream.Create(''); MStream.LoadFromFile(ThumbNail); Stream.CopyFrom(MStream, MStream.Size); FActiveGrid.DataController.SetValue(RowInfo.RecordIndex, ThumbCol, Stream.DataString);
改進後的:
function StreamToVar(Stream: TStream): OleVariant; var P: Pointer; begin Result := VarArrayCreate([0, Stream.size -1],Varbyte); P := VarArrayLock(Result); Try Stream.Position := 0; Stream.Read(P^, Stream.size); Finally VarArrayUnlock(Result); end; end; procedure TForm1.Button1Click(Sender: TObject); var IRecIdx : Integer; stream : TMemoryStream; begin with cxGrid1TableView1.DataController do begin IRecIdx := AppendRecord; stream := TMemoryStream.Create(); stream.LoadFromFile('H:\pic\隨拍\IMAG0002.jpg'); stream.Position := 0; Values[IRecIdx,0] := StreamToVar(stream); stream.Free; Post; end; end;
你可以在紅叉上點擊右鍵,看看圖片屬性,有個地址,先自己判斷下地址對不對,一般這樣的問題就是路徑設置的問題,相對路徑絕對路徑要注意。我這裡有這樣的代碼,如果你需要可以找我要。
你可以參考這個問題
zhidao.baidu.com/question/114371806.html
內容差不多,比方說,dgvOperation為一個DataGridView,第一列為DataGridViewCheckBoxColumn ,你需要選中1,3,4列,就這樣做:
for (int i = 0; i < dgvOperation.Rows.Count; i++)
{
if((i+1)==1||(i+1)==3||(i+1)==4)
{
//設置選擇框為選中,第一列為checkbox
dgvOperation.Rows[i].Cells[0].Value = true;
}
}
程序寫得比較粗糙,你可以再優化一下,具體的操作就是這樣的了:)