LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Swapping between 2 rows in a table control

Solved!
Go to solution

How do you swap 2 rows in a table without any lagging effect?

 

Currently, I copied all the data from one row to a temporary buffer, overwrite the row with the row to be swapped. Then overwrite the swapped row with the data in the temporary buffer, something like the bubble-sorting algorithm.

 I only used the functions "GetTableCellVal" and "SetTableCellVal" with several 'for' loops.

 

I am able to swap between the rows but the swap takes quite some time.

Any idea how to swap the rows without any significant time delay?

0 Kudos
Message 1 of 5
(4,085 Views)

There are a couple of hints that can speed up table access.

First of all, using GetTableCellRangeVals should be faster than addressing individual cells in a loop; a limitation of this command is that all cells must have the same data type, but you could try using it every time there are contiguous cells with the same data type with beneficial effects.

A second way of speeding up table update is to hide it Smiley Surprised : you could clear the visible attribute before the table update fragment of code and set it again immediately after it; this will have no effect on the screen but will significantly speed up the process. To make this trick work correctly the table must not be the active control, so you must use SetActiveCtrl to switch the focus to another control.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 5
(4,071 Views)
Solution
Accepted by topic author leekahwai

If all the cells in each row have the same cell type (and data type, if they're numeric) then you could use GetTableCellRangeVals / SetTableCellRangeVals instead, which should speed things up.

 

Another trick is to hide the table temporarily (ATTR_VISIBLE attribute) around the entire operation. This prevents SetTableCellVal or SetTableCellRangeVals from triggering a redraw of the table. Redraws can be slow, and in this case you certainly don't need any drawing until you're done with the swap. When you make the table visible again, it will redraw automatically the next time events are processed.

 

An alternative to setting and getting each value is to use the table clipboard functions. I really don't know if it would be faster, but you can test it and find out. The code would look something like this:

 

    GetNumTableRows (panel, PANEL_TABLE, &numRows);
    InsertTableRows (panel, PANEL_TABLE, numRows+1, 1, VAL_USE_MASTER_CELL_TYPE);
    ClipboardPutTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(row1));
    ClipboardGetTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(numRows+1), NULL);
    ClipboardPutTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(row2));
    ClipboardGetTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(row1), NULL);
    ClipboardPutTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(numRows+1));
    ClipboardGetTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(row2), NULL);
    DeleteTableRows (panel, PANEL_TABLE, numRows, 1);
 

It wasn't clear from your description whether you can assume that the two rows to be swapped are always adjacent. If they are, then the code above could be made much simpler, since in that case you only need to make one leapfrog copy:

 

    InsertTableRows (panel, PANEL_TABLE, topRow+2, 1, VAL_USE_MASTER_CELL_TYPE);
    ClipboardPutTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(topRow));
    ClipboardGetTableVals (panel, PANEL_TABLE, VAL_TABLE_ROW_RANGE(topRow+2), NULL);
    DeleteTableRows (panel, PANEL_TABLE, topRow, 1);

 

Hope this helps,

Luis

 

 

 

Message 3 of 5
(4,068 Views)

Darn... took too long, and was foiled by Robert once again!

 

Smiley Happy

 

 

 

0 Kudos
Message 4 of 5
(4,067 Views)

Hi Robert, Luis

 

  Both methods work! And both are equally fast for my cause.

However, my table doesn't have the same datatype for each cell. Therefore, I will just stick to Luis method.

 

Thanks a lot! 

 

   

0 Kudos
Message 5 of 5
(4,039 Views)