Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to determine specific row in string grid 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have two string grids on a form, sgSelected and sgAvailable.

sgSelected shows:

AM 050620P01
AM 050620P02
PM 050620P03

sgAvailable shows:

050620P01
050620P02
050620P03
050627P04
050627P05
050627P06

I want to have a drag and drop procedure for the user to select a panel from sgAvailable and drop it to a specific cell in sgSelected. I have the following code which always replaces the first cell of information in sgSelected, but what I want is for the information to be replaced in the specific cell it's being dragged to. For instance, if I click on 050627P06 and drag it to the second cell in sgSelected I would want 050620P02 replaced with 050627P06.

Any idea of what I need to do to make this happen? Thanks!

Leslie

Code:
procedure TfrmChangePanels.sgAvailableSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  Self.SelectedRow := ARow;  //SelectedRow public variable integer
  sgAvailable.BeginDrag (false,5);
end;


procedure TfrmChangePanels.sgSelectedDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  src: TStringGrid;
  dst: TStringGrid;
  i : integer;
begin
  if (source is TStringGrid) and (sender is TStringGrid) then begin
    src := source as TStringGrid;
    dst := sender as TStringGrid;

    dst.Cells[1, 0] := src.Cells[0, Self.SelectedRow];

  end;
end;

procedure TfrmChangePanels.sgSelectedDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TStringGrid;
end;

 

You should be able to use the MouseToCell method to convert the X,Y coords to a cell reference.

 
That worked great! For anyone who may need something similar, here's what I ended up using:

Code:
procedure TfrmChangePanels.sgSelectedDragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TStringGrid;
end;

procedure TfrmChangePanels.sgSelectedDragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  src: TStringGrid;
  dst: TStringGrid;
  i : integer;
  procedure UpdatePanels(Session : String; Panel : string; UpdateInfo : string);
  begin
    with qryChange do
    begin
      SQL.Clear;
      SQL.Add('UPDATE JMPDLYPANL SET PANELID = ' + QuotedStr(UpdateInfo) + ' WHERE SESSION = ' + QuotedStr(Session) + ' AND PANELID = ' + QuotedStr(Panel) + ' AND SERVDAT = ' + QuotedStr(FormatDateTime('YYYYMMDD', Date())));
      ExecSQL;
    end;
  end;
begin
  if (source is TStringGrid) and (sender is TStringGrid) then begin
    src := source as TStringGrid;
    dst := sender as TStringGrid;
    sgSelected.MouseToCell(X, Y, NewColumn, NewRow);
    CurrentSession := dst.Cells[0, NewRow];
    CurrentPanel := dst.Cells[1, NewRow];
    dst.Cells[1, NewRow] := src.Cells[0, OldRow];
    UpdatePanels(CurrentSession, CurrentPanel, NewPanel);
    //showMessage('Current Session = ' + CurrentSession + '; Current Panel = ' + CUrrentPanel + '; New Panel = ' + NewPanel);
  end;
end;

procedure TfrmChangePanels.sgAvailableSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  sgAvailable.BeginDrag (false,5);
end;

procedure TfrmChangePanels.sgAvailableMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  sgAvailable.MouseToCell(X, Y, OldColumn, OldRow);
  NewPanel := sgAvailable.Cells[OldColumn, OldRow];
end;

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
Les,

May I suggest that you write this as an FAQ? This will make it easier for others to find the solution in the future.

The other advantage of FAQs is that the author can tidy, correct or otherwise amend the solution over a period of time. For example, you might think that your solution would be tidier without the //showMessage comment.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top