Jump to content
Sign in to follow this  
IElite

getting the row # of a button in a cell of a TW3ButtonColumn in Tw3Grid ?

Recommended Posts

whew, that's a mouth full  "getting the row # of a button in a cell of a TW3ButtonColumn in Tw3Grid ?"

So, I have a TW3Grid, and a TW3ButtonColumn. 

When the button in the column is clicked, I need to know the row # that button resides in.

 

see attached screenshot

 

screenshot.png

Share this post


Link to post
Share on other sites

every time you add a row, fill the tagid of the row with the row number. TagId is a field (string) present in all component descendants in which you can save things. 

Then in an onclick handler read that field to retrieve the clicked on rownr.

    (MyGrid.Row[MyGrid.LastAddedRow] as TW3CustomControl).TagId := inttostr(MyGrid.LastAddedRow);

    (MyGrid.Row[MyGrid.LastAddedRow] as TW3CustomControl).onClick := procedure(sender:TObject)
    begin
      browserapi.window.alert((Sender as TW3CustomControl).TagId);
    end;

(Alternatively you can use the data- attribute as temporary storage).

Share this post


Link to post
Share on other sites

ah, why didnt i think of that?  :)  THANKS!

think they should add a property or method for getting the row though :)   also, access to the button or any TW3CustomControl that resides in the column for that matter 

 

Share this post


Link to post
Share on other sites

I jumped the gun @lynkfs .....that does not help me solve the original issue of how to determine the row from the button clicked in the button column.

With your code, I was able to flag the row with the id, but I still dont have access to it from the button. I assume once I can figure that out, I can  figure our the row number anyways, with out the need to store anything in TagId

procedure TSelGrid.Add(AGrade, AClasses, AStudents, AProgram: String);
begin
 fGrid.AddRow;

 (fGrid.Row[fGrid.LastAddedRow] as TW3CustomControl).TagId := inttostr(fGrid.LastAddedRow);
 

fGrid.Row[fGrid.LastAddedRow].Color := clWhite;
 fGrid.Row[fGrid.LastAddedRow].Border.Style:= besSolid;
 fGrid.Cell[0, fGrid.LastAddedRow].Value := AGrade;
 fGrid.Cell[1, fGrid.LastAddedRow].Value := AClasses;
 fGrid.Cell[2, fGrid.LastAddedRow].Value := AStudents;
 fGrid.Cell[3, fGrid.LastAddedRow].Value := AProgram;
 fGrid.Cell[4, fGrid.LastAddedRow].Value := 'Edit';
 fGrid.Cell[5, fGrid.LastAddedRow].Value := 'Delete';
end;

 

Share this post


Link to post
Share on other sites

Update......couple changes

changed TagId to TagValue  (work with integers instead of strings) when prepping the row

procedure TSelGrid.Add(AGrade, AClasses, AStudents, AProgram: String);
begin
 fGrid.AddRow;
 (fGrid.Row[fGrid.LastAddedRow] as TW3CustomControl).TagValue := fGrid.LastAddedRow;
 ''''''
end;

Next, in order to get the row index from the TagValue property, I just did a cascading look into all the parents

Button --> Cell -- > Row

procedure TSelGrid.HandleDeleteClick(sender: TObject);
var
 RowId: integer;
begin
  RowId:= Tw3Row(TW3Cell(TW3Button(sender).Parent).Parent).TagValue;
 fGrid.DeleteRow(RowId);
end;

 

I would still be interested in knowing if there is an easier solution to get the row.  Maybe add the property to the grids TW3Cell? TW3Column

 

Share this post


Link to post
Share on other sites

In the next release there's a new OnCellControlCreated -event in TW3Column. It can be used to set properties for the button. For example:

  ButtonCol := TW3ButtonColumn.Create(W3Grid1 as IW3ColumnsControl);
  ButtonCol.OnButtonClick := procedure(Sender: TObject)
    begin
      WriteLn('Click on row '+IntToStr(TW3Button(Sender).TagValue));
    end;
  ButtonCol.OnCellControlCreated := procedure(Sender: TObject)
    begin
      TW3Button(Sender).TagValue:=W3Grid1.RowCount;
    end;

 

Share this post


Link to post
Share on other sites
58 minutes ago, jarto said:

In the next release there's a new OnCellControlCreated -event in TW3Column. It can be used to set properties for the button. For example:


  ButtonCol := TW3ButtonColumn.Create(W3Grid1 as IW3ColumnsControl);
  ButtonCol.OnButtonClick := procedure(Sender: TObject)
    begin
      WriteLn('Click on row '+IntToStr(TW3Button(Sender).TagValue));
    end;
  ButtonCol.OnCellControlCreated := procedure(Sender: TObject)
    begin
      TW3Button(Sender).TagValue:=W3Grid1.RowCount;
    end;

 

Awesome!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×