可以在数据控件的
BeforePost事件中写入代码
if 字段=null then 
    Begin
    Application.MessageBox('字段不能为空','错误!',0);
    Abort;
    end;

解决方案 »

  1.   

    以下是我的一段程序,
    你只要一个myDBGrid.SelectedIndex就够了,
    它表示刚刚退出的列是第几列,0:表示第一列,1:表示第二列。procedure TFSell.myDBGridColExit(Sender: TObject);
    var
      tmp : ST_WARECODE;
      TotalNum : Integer;
      FactPrice : Single;
    begin
      inherited;
      if ((myTable.State<>dsEdit) and (myTable.State<>dsInsert)) then
        exit;  if myDBGrid.SelectedIndex=0 then//SelectedIndex就是第几列!!!!!!!
      begin
        tmp.sWareCode := myTable.FieldByName('WareCode').AsString;
        if Trim(tmp.sWareCode)='' then
          exit;
        if not MainDataModule.GetWareInfo(tmp) then
        begin
          WARNING('货号:“'+tmp.sWareCode+'" 不存在');
        end
        else
        begin
          if (tmp.sBrandCode<>pData.pCurrentBrand.sBrandCode)then
          begin
            WARNING('货号:“'+tmp.sWareCode+'" 的品牌不是"'+pData.pCurrentBrand.sBrandName+'"');
            exit;
          end;
          myTable.FieldByName('WareName').AsString := tmp.sWareName;
          myTable.FieldByName('Price').AsFloat := tmp.fPrice;
          myTable.FieldByName('FactPrice').AsFloat := tmp.fPrice*tmp.fDiscount/100;
          myTable.FieldByName('CostPrice').AsFloat := tmp.fCostPrice;
        end;
        GetKCSL;
        exit;
      end;  with myDataSource.DataSet do
      begin
        TotalNum := FieldByName('Size_XS').AsInteger
                     +FieldByName('Size_S').AsInteger
                     +FieldByName('Size_M').AsInteger
                     +FieldByName('Size_L').AsInteger
                     +FieldByName('Size_XL').AsInteger
                     +FieldByName('Size_XXL').AsInteger
                     +FieldByName('Size_Other').AsInteger;
        if ((myDBGrid.SelectedIndex>=2) and (myDBGrid.SelectedIndex<=8))and((FuncClass<>tfLookAbout) and ((State=dsEdit) or (State=dsInsert))) then
        begin
          FieldByName('TotalNum').AsInteger := TotalNum;
          try
            FactPrice := FieldByName('FactPrice').AsFloat;
            FieldByName('TotalFactMoney').AsFloat := StrToFloat(Format('%.3f',[TotalNum*FactPrice]));
          except
          end;
          exit;
        end;
        try //防止除零!
          if (myDBGrid.SelectedIndex=11)and((FuncClass<>tfLookAbout) and ((State=dsEdit) or (State=dsInsert))) then
          begin//折扣
            FactPrice := FieldByName('Price').AsFloat * FieldByName('Discount').AsFloat / 100;
            FieldByName('FactPrice').AsFloat := StrToFloat(Format('%.3f',[FactPrice]));
            FieldByName('TotalFactMoney').AsFloat := StrToFloat(Format('%.3f',[TotalNum*FactPrice]));
            exit;
          end;      if (myDBGrid.SelectedIndex=12)and((FuncClass<>tfLookAbout) and ((State=dsEdit) or (State=dsInsert))) then
          begin//实销价
            FactPrice := FieldByName('FactPrice').AsFloat;
            FieldByName('Discount').AsFloat := StrToFloat(Format('%.3f',[FactPrice / FieldByName('Price').AsFloat * 100]));
            FieldByName('TotalFactMoney').AsFloat := StrToFloat(Format('%.3f',[TotalNum*FactPrice]));
            exit;
          end;
          if (myDBGrid.SelectedIndex=13)and((FuncClass<>tfLookAbout) and ((State=dsEdit) or (State=dsInsert))) then
          begin//实销金额
            FactPrice := FieldByName('TotalFactMoney').AsFloat / TotalNum;
            FieldByName('Discount').AsFloat := FactPrice / FieldByName('Price').AsFloat * 100;
            FieldByName('FactPrice').AsFloat := FactPrice;
            exit;
          end;
        except
        end;
      end;
    end;