?
Solved

DBGrid without Table

Posted on 1999-12-09
8
Medium Priority
?
544 Views
Last Modified: 2010-04-06
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).
0
Comment
Question by:gianluca
8 Comments
 
LVL 1

Expert Comment

by:men xin
ID: 2269130
qianloca:

You may use the TStringGrid.

menxin
0
 

Expert Comment

by:BSamson
ID: 2269199
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
 
LVL 4

Expert Comment

by:mrorph
ID: 2269386
I used TStringGrid with combos and spinners as well as Insert and Delete functionality.  

If you like, I will post some code snippets.  

0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LVL 1

Expert Comment

by:Phoenix_s
ID: 2269946
TStringGrid... definately
0
 

Author Comment

by:gianluca
ID: 2270951
Mrorph, please post me some sample code.
Thanks,
0
 
LVL 4

Expert Comment

by:mrorph
ID: 2271550
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
 

Author Comment

by:gianluca
ID: 2274283
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
 
LVL 4

Accepted Solution

by:
mrorph earned 150 total points
ID: 2279840
>>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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question