DBGrid without Table

How is possible use a DBGrid without a table?? I would like to use a DBGrid to enter strings with a combo-box, column title, and insert rows functionality. I can create the fields with their property in the columns-editor of the DBGrid, but at run-time it seems it can not allows edits ("Option" property is set to dsEditing).
gianlucaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

men xinCEOCommented:
qianloca:

You may use the TStringGrid.

menxin
0
BSamsonCommented:
Download a copy of the latest RXLib 2.75 from www.rxlib.com

It's freeware, and certainly worth putting in your component palette.

Drop a RXLib - TMemoryTable ( BDE independant ) on your form or DataModule, Use the fields editor to add any fields you require to the dataset ( This can also include Blobs ). link the TMemoryTable to the DBGrid via a DataSource component.

Then just use it like you would an ordinary Dataset.

Failing that use a TListView component instead of a Grid.  They're very powerful once you get the hang of them.

Good Luck
Brian
0
mrorphCommented:
I used TStringGrid with combos and spinners as well as Insert and Delete functionality.  

If you like, I will post some code snippets.  

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Phoenix_sCommented:
TStringGrid... definately
0
gianlucaAuthor Commented:
Mrorph, please post me some sample code.
Thanks,
0
mrorphCommented:
On a form I have a TStringGrid.  I dropped 2 spinEdit controls and a combo box on the form and set their visible properties to false.
Keep in mind that this grid is used to read in the structure of an xBase table.  The user can then modify the structure. I use Halcyon to access the xBase tables.

//Load the TStringGrid with the table structure...
procedure TfrmTableProperties.LoadFields;
var
  j, TotalLen: Integer;
begin

  CurrentRow := 1;
  EditRow  := -1;
  TotalLen := 1;

  with tblFldData, StringGrid1 do
  begin

    //Column headers
    RowCount := succ(FieldCount);
    Cells[0,0] := 'Number';
    Cells[1,0] := 'Name';
    Cells[2,0] := 'Type';
    Cells[3,0] := 'Length';
    Cells[4,0] := 'Decimal';

    FixedRows := 1;

    //Rows
    for j := 1 to FieldCount do

      with DBFHandle do
      begin

        Cells[0,j] := IntToStr(Fields[j].dbFieldNum);
        Cells[1,j] := Fields[j].dbFieldName;
        Cells[2,j] := Fields[j].dbFieldType;
        Cells[3,j] := IntToStr(Fields[j].dbFieldLgth);
        Cells[4,j] := IntToStr(Fields[j].dbFieldDec);

        TotalLen := TotalLen + Fields[j].dbFieldLgth;

       end;//with

       Inc(j);

    end; //with

  end;    // with

end;

This code block is the most significant.  When the user enters a column, I paint the appropriate control in the cell:

procedure TfrmTableProperties.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  tmpStr : String;
  tmpRect :TRect;
begin

  case ACol of

    1:
    begin
      //Be sure that the controls are not visible
      HideGridControls;
      Exit;
    end;

    // Type
    2:
    begin
      with cboType do
      begin
        if (ACol >= StringGrid1.FixedCols) and
         (ARow >= StringGrid1.FixedRows) and
         (gdFocused in State) then
          begin
            spnDecimal.Hide;
            spnLength.Hide;
            tmpStr := StringGrid1.Cells[ACol,ARow];
            SetWindowPos(cboType.Handle, // handle
                         0, // change z-order
                         Rect.Left + StringGrid1.Left + 1, // left
                         Rect.Top + StringGrid1.top + 1, // top
                         Rect.Right - Rect.Left + 2, // width
                         Rect.Bottom - Rect.top + 2, // height
                         SWP_NOZORDER      or SWP_SHOWWINDOW); // flags
            cboType.Text := tmpStr;
            cboType.Show;
            Exit;
        end;
      end;    // with
    end;

    // Length
    3:
    begin
      with spnLength do
      begin
        if (ACol >= StringGrid1.FixedCols) and
           (ARow >= StringGrid1.FixedRows) and
           (gdFocused in State) then
          begin
            cboType.Hide;
            spnDecimal.Hide;
            tmpStr := StringGrid1.Cells[ACol,ARow];
            SetWindowPos(spnLength.Handle, // handle
                         0, // change z-order
                         Rect.Left + StringGrid1.Left + 1, // left
                         Rect.Top + StringGrid1.top + 1, // top
                         Rect.Right - Rect.Left + 2, // width
                         Rect.Bottom - Rect.top + 2, // height
                         SWP_NOZORDER      or SWP_SHOWWINDOW); // flags
            spnLength.Text := tmpStr;
            spnLength.Show;
            Exit;
         end;
      end;     // with
    end;

    // Decimal
    4:
    begin
      with spnDecimal do
      begin
      if (ACol >= StringGrid1.FixedCols) and
         (ARow >= StringGrid1.FixedRows) and
         (gdFocused in State) then
        begin
          spnLength.Hide;
          cboType.Hide;
          tmpStr := StringGrid1.Cells[ACol,ARow];
          SetWindowPos(spnDecimal.Handle, // handle
                       0, // change z-order
                       Rect.Left + StringGrid1.Left + 1, // left
                       Rect.Top + StringGrid1.top + 1, // top
                       Rect.Right - Rect.Left + 2, // width
                       Rect.Bottom - Rect.top + 2, // height
                       SWP_NOZORDER      or SWP_SHOWWINDOW); // flags
          spnDecimal.Text := '0';
          spnDecimal.Show;
          Exit;
        end;
      end;     // with
    end;
  end;    // case
end;


This will Insert a new row into the grid.  TStringGrid inherits these methods from TCustomGrid but they are protected.  

procedure MoveColumn(FromIndex, ToIndex: Longint);
    procedure MoveRow(FromIndex, ToIndex: Longint);
    procedure InvalidateCell(ACol, ARow: Longint);
    procedure InvalidateCol(ACol: Longint);
    procedure InvalidateRow(ARow: Longint);
    procedure DeleteColumn(ACol: Longint); virtual;
    procedure DeleteRow(ARow: Longint); virtual;
 
We get to them using a cracker class hack:
 
type
  TExStringGrid = Class(TStringGrid);

procedure TfrmTableProperties.InsertGridRow(GridControl: TStringGrid; AtIndex: Integer);
begin

  Assert(Assigned(GridControl));

  with TExStringGrid(GridControl) do
  begin

    if AddToBottom then
    begin
      RowCount := RowCount + 1;
      Row := RowCount - 1;
      AddToBottom := False;
    end
    else
    begin
      while AtIndex < FixedRows do Inc(AtIndex);
        RowCount := RowCount + 1;
        MoveRow(Rowcount, atIndex);
      if Row <> RowCount then Row := Row - 1;
      if Row = 0 then Row := 1;
    end;

  end;    // with

end;

To delete a row use:
TExStringGrid(StringGrid1).DeleteRow(StringGrid1.Row);

Hope this helps.

0
gianlucaAuthor Commented:
Mrorph, thanks for your code for your code. I found it very ufesul but I have one more question : how can I fill the cell with the text from the combo-box ?? Is there a way to know when the cell loose focus (or selection) to perform a range check ???
0
mrorphCommented:
>>how can I fill the cell with the text >>from the combo-box ??

You have to assign the text value of the combo to the grid cell.  Since I am using InfoPower, the combo has a OnCloseUp event.  Using the native Delphi combo, you may want to use the OnChange event.

>>Is there a way to know when the cell >>loose focus (or selection) to perform >>a range check ???

In essence, the DrawCell gives you that ability. As the highlight traverses the grid, the DrawCell fires.  You don't necessarily know when a column does not have focus but you know when it does.

You handle all of your validation code in the control itself.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.