Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Deleting lines from TstringGrid

bubblybubbly Member Posts: 18
I have a TStringGrid that the user populates themselves, but how do I allow them to delete an entire line?

At the moment all I can do is manually delete each cell and then drag the line to the bottom.

I can't use rowselect because this won't allow me to access each cell individually.

What I want is, when the user clicks a 'Delete' button, I want my program to delete the whole line

Any ideas?

Comments

  • zibadianzibadian Member Posts: 6,349
    : I have a TStringGrid that the user populates themselves, but how do I allow them to delete an entire line?
    :
    : At the moment all I can do is manually delete each cell and then drag the line to the bottom.
    :
    : I can't use rowselect because this won't allow me to access each cell individually.
    :
    : What I want is, when the user clicks a 'Delete' button, I want my program to delete the whole line
    :
    : Any ideas?
    :
    Here is a tested code, which will delete the line, in which the selected cell is:
    [code]
    with StringGrid1 do
    begin
    // First move all lines below the one to delete up 1 line
    if Selection.Top < RowCount-1 then
    for j := Selection.Top to RowCount-2 do
    Rows[j] := Rows[j+1];
    // Then remove the last line
    RowCount := RowCount-1;
    end;
    [/code]
    It moves the line to be deleted down by moving all the lines below it up. This way the last line becomes the line to be deleted. This can easily be done by decrementing the RowCount property.
    Inserting a line can be done in a similar way:
    - First add a new line by incrementing the RowCount
    - Then move the lines down (lowest first, highest last)
    - Clear the selected line to simulate a new line.
Sign In or Register to comment.