单元1,下面函数用于取得Order表中的ID,Date等字段到Variant变量。
Function TDMOrder.GetOrderMasterData:Variant;
var
FromDate, ToDate: TDate;
i: Integer;
begin
FromDate := EnCodeDate(FromYear, FromMonth, 1);
ToDate := GetYearMonthLastDate(ToYear, ToMonth);//this function declare in datamodule's parent
with qryOrder do
begin
Close;
Parameters.ParamByName('FromDate').Value := FromDate;
Parameters.ParamByName('ToDate').Value := ToDate;
Open;
i := 1;
Result := VarArrayCreate([1, RecordCount, 0,4], varOleStr);//详见VarArrayCreate帮助,以悉VarOleStr等的作用
While Not Eof do
begin
Result[i, 0] := FieldByName('ID').AsString;
Result[i, 1] := DateToStr(FieldByName('InDate').AsDateTime);
// Result[i, 1] := FieldByName('InDate').AsString;
Result[i, 2] := FieldByName('OurStyleNo').AsString;
Result[i, 3] := FieldByName('Remark').AsString;
Inc(i);
Next;
end;
end;
end;
下面函数用于调用刚才的函数,获得Variant变量,并显示到StringGrid中,
上面的函数我是做为一个COM对象里的一个方法的,所以下面的函数建立了一个COM对象
至于COM对象的做法,不在这里讨论。
procedure TfrmOrder.GetOrderData;
var
FromYear, FromMOnth,
ToYear, ToMonth: Word;
OrderData: OleVariant;
Order: IOrder;
i, OrderCount: integer;
begin
FromYear := StrToInt(framYearMonth1.cmbYear.Text);
FromMonth := StrToInt(framYearMonth1.cmbMonth.Text);
ToYear := StrToInt(framYearMonth2.cmbYear.Text);
ToMonth := StrToInt(framYearMonth2.cmbMonth.Text);
Order := CoOrder.Create;
Order.GetOrderData(FromYear, FromMOnth, ToYear, ToMonth, OrderData);
if VarIsNull(OrderData) then Exit;
//ini StringGrid if没有记录查到
sgOrder.RowCount := 2;
sgOrder.Rows[1].Clear;
OrderCount := VarArrayHighBound(OrderData, 1);//此处用于获得数组的个籽,看VarArrayHighBound方法以悉其用法。注意里面用1的目的。
if OrderCount <= 0 then Exit;
sgOrder.RowCount := OrderCount + 1;
for i:= 1 to OrderCount do
with sgOrder do
begin
Rows.Clear;
Rows.Add(OrderData[i, 0]);//逐个取得数组中的值,放中Cell
Rows.Add(OrderData[i, 1]);
Rows.Add(OrderData[i, 2]);
Rows.Add(OrderData[i, 3]);
end;
//focus到上次选择的那条裁单记录
if sgOrder.RowCount - 1 >= RowSelected then
sgOrder.Row := RowSelected;
end;