cxGrid 鎖定一行,讓該行數據不能編輯


在使用cxGrid時,由於設置了所有單元格都能編輯,

但在特定的情況下,讓某些行,根據一些列值條件,讓該行整行鎖定,不能編輯。

研究了很久,在DevExpress官網上找到了相關的資料,因此,分享給大家。

Dev官網的列子是這樣的

// DISABLE A ROW  整行禁止編輯
procedure TForm1.cxGrid1DBTableView1Editing(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; var AAllow: Boolean);
var
  AKeyValue : Variant;
begin
  AKeyValue := Sender.DataController.GetRecordId(Sender.Controller.FocusedRecordIndex);
  if (AKeyValue = '1351') or (AKeyValue = '1356') or (AKeyValue = '1384') then
    AAllow := False;
end;

 

// MAKING A ROW READ ONLY   讓一行只讀
procedure TForm1.cxGrid1DBTableView1InitEdit(
  Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem;
  AEdit: TcxCustomEdit);
var
  AKeyValue : Variant;
begin
{  AKeyValue := Sender.DataController.GetRecordId(Sender.Controller.FocusedRecordIndex);
  if (AKeyValue = '1351') or (AKeyValue = '1356') or (AKeyValue = '1384') then
    AEdit.ActiveProperties.ReadOnly := True;}
end;

 

// MAKING A ROW LOOK LIKE DISABLED   讓一行看起來禁止了
procedure TForm1.cxGrid1DBTableView1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  AKeyValue : Variant;
begin
  AKeyValue := Sender.DataController.GetRecordId(ARecord.RecordIndex);
  if (AKeyValue = '1351') or (AKeyValue = '1356') or (AKeyValue = '1384') then
    AStyle := cxDisableStyle;
end;

在實際使用中,若要根據某列的值,控制該行是否可編輯,代碼如下:

procedure TForm1.cxGrid1DBTableView1Editing(Sender: TcxCustomGridTableView;

    AItem: TcxCustomGridTableItem; var AAllow: Boolean);
begin

 //cxGrid1DBTableView1CanEdit 為cxGrid中某列,判斷不為空時,設置該行不能編輯。
  if VarToStrDef(Sender.Controller.FocusedRecord.Values[cxGrid1DBTableView1CanEdit.Index], '') <> '' then
    AAllow := False;
end;
 
 
cxGrid 鎖定一行,只能編輯該行數據
 
cxGrid中如何把鼠標鎖定在一行上,不能讓它上下移動,在瀏覽狀態下用戶可以上下移動記錄,但執行"修改"后就把鼠標鎖定在當前修改的記錄上,直接執行"保存"后才解除鎖定.

在TcxGridDBTableView的OnCanFocusRecord事件中寫代碼就行了.如下:

procedure TFrmBillType.tvBillTypesCanFocusRecord(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  var AAllow: Boolean);
begin
  inherited;
  if bUInEdit then
    AAllow := false;
end;

 

轉載於: https://www.cnblogs.com/K-R-/p/6913211.html


免責聲明!

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



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