//from
http://kingron.myetang.com/zsfunc0s.htm(*//
标题:字符网格和文本间转换
说明:StringGrid
设计:Zswang
日期:2002-02-21
支持:[email protected]
//*)///////Begin Source
uses
  Math;function StrLeft(const mStr: string; mDelimiter: string): string;
begin
  Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
var
  I, L, K: Integer;
begin
  L := Length(mList);
  I := Pos(mDelimiter, mList);
  K := 0;
  Result := '';
  while (I > 0) and (K <> mIndex) do begin
    mList := Copy(mList, I + Length(mDelimiter), L);
    I := Pos(mDelimiter, mList);
    Inc(K);
  end;
  if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter);
end; { ListValue }function StringGridToText(mStringGrid: TStringGrid;
  mStrings: TStrings): Boolean;
var
  I, J: Integer;
  T: string;
begin
  Result := False;
  if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;
  with mStringGrid do try
    mStrings.Clear;
    for J := 0 to RowCount - 1 do begin
      T := '';
      for I := 0 to ColCount - 1 do
        T := T + #9 + Cells[I, J];
      Delete(T, 1, 1);
      mStrings.Add(T);
    end;
  except
    Exit;
  end;
  Result := True;
end; { StringGridToText }function TextToStringGrid(mStrings: TStrings;
  mStringGrid: TStringGrid): Boolean;
var
  I, J: Integer;
  T: string;
begin
  Result := False;
  if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;
  with mStrings, mStringGrid do try
    for I := 0 to ColCount - 1 do begin
      T := '';
      for J := 0 to Min(RowCount - 1, Count - 1) do
        Cells[I, J] := ListValue(Strings[J], I, #9);
    end;
  except
    Exit;
  end;
  Result := True;
end; { TextToStringGrid }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
  StringGridToText(StringGrid1, Memo1.Lines);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  TextToStringGrid(Memo1.Lines, StringGrid1);
end;
///////End Demo//保存
StringGridToText(StringGrid1, vStringList);
Table1.Edit;
Table1['GridText'] := vStringList.Text;//载入
vStringList.Text := Table1.FieldByName('GridText').AsString;
TextToStringGrid(vStringList, StringGrid1);

解决方案 »

  1.   

    没什么特别的要求,就是要求写在edit的keypress事件中。写出你的做法。
    拜托。
      

  2.   

    Var 
     Ri,Ci:integer;//行和列的数量
     sSQL:string;//SQL
    for Ri:=0 to Ri do
     begin
       sSql:='insert A(字段1,字段2,字段3....)Values('''+trim(StrGrid.Cell[0,Ri])+'''(第0行0列的值),'''+trim(StrGrid.Cell[1,Ri])+'''(第1列第0行),)';以下的写法类似
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add(sSql);
        ExecSQL;
       end;
     end;