Page 1 of 1

TableView edit : 'freeze' column(s) ?

Posted: Wednesday 10th March 2021 8:50am
by Doctor Watson
In my project I need to enter data using a ‘simple’ spreadsheet. TableView could be just perfect for that if it’s possible to :
1) set one or two columns to ‘Read Only’ , so that it’s impossible to change the contents within.
2) sort the data, because I read in the help files :

Control.:sort (gb.qt5)?
This symbol does not exist.

With GridView it seems to be possible, but then again, that control is not editable.
The same problem existed – years ago – in RealBasic and I found a solution, but if TableView can do the trick, that’s a lot of programming effort less.

Re: TableView edit : 'freeze' column(s) ?

Posted: Wednesday 10th March 2021 2:17pm
by 01McAc
Hi Doctor,

I think it shouldn't be to complicated. Unfortunately, I am not good with tableviews but I fiddled around with these tableview example (attached). Source of the example is https://gambas-buch.de/dwen/doku.php?id=k17:k17.8:start
I set column 1 of the tableview to readonly by cancelling the event. Have a look at the code.
Public Sub TableView1_Click()
   'If cbxEditModus.Value = False Then
   '   TableView1.Edit()
   'Else
      Select Case TableView1.Column
        Case 0
          TableView1.Edit(["Linux", "Windows", "OS-X"])
        Case 1
          Stop Event  'Event needs to be stopped and a Return is necessary
          Return
        Case 2
          TableView1.Edit(["Desktop-PC", "NoteBook", "NetBook", "Tablet PC"], True)
      End Select
   'Endif ' cbxEditModus.Value = False ?
End ' TableView1_Click()

Re: TableView edit : 'freeze' column(s) ?

Posted: Wednesday 10th March 2021 2:29pm
by 01McAc
WRT Sorting: just turn on the sorting in the properties.

Code: Select all

Tableview1.Sort=true
You need to add your own sort algorithm into the sort event
Public Sub TableView1_Sort()

  Dim Values, ValueSorted As New String[]
  Dim Nx, iNx As Integer

  ' Load sort column into string array
  For Nx = 0 To TableView1.Rows.Max
    Values.Add(TableView1[Nx, TableView1.Columns.Sort].Text)
  Next

  ' Copy values to new array and sort it based on status of sort indicator
  ValueSorted = Values.Copy()
  ValueSorted.Sort(IIf(TableView1.Columns.Ascending, gb.Ascent, gb.Descent))

  ' Iterate through table swapping appropriate values
  For Nx = 0 To ValueSorted.Max
    For iNx = 0 To TableView1.Columns.Max
      Swap TableView1[Nx, iNx].Text, TableView1[Values.Find(ValueSorted[Nx], 0, Nx), iNx].Text
    Next

    ' Pick up new order to preserve proper handling of indexing and duplicate values
    Values.Clear()
    For iNx = 0 To TableView1.Rows.Max
      Values.Add(TableView1[iNx, TableView1.Columns.Sort].Text)
    Next

  Next

  TableView1.Refresh()

End

Re: TableView edit : 'freeze' column(s) ?

Posted: Wednesday 10th March 2021 2:58pm
by BruceSteers
Looking at the wiki page http://gambaswiki.org/wiki/comp/gb.form/tableview

It seems you must explicitly call an "Edit" event to edit a cell.

So in this call you could be able to detect the current row index and just stop the event call and return out of the Edit function.

wiki page says Edits do not happen automatically so you have control of what edits and what does not.
wiki wrote:How to make a cell editable

To make a cell editable, you must call the Edit or EditWith method in response to a Click or Activate event.

Calling one of this method creates a cell editor widget that allows the user to modify the cell contents. This editor is automatically managed by the TableView.

If the user modifies the cell contents, then the Save event is raised, and you must actually save the data during the Save event handler. This is not automatic!

If you want to explicitly cancel an edition, call the Cancel method.
Bruce

Re: TableView edit : 'freeze' column(s) ?

Posted: Wednesday 10th March 2021 5:28pm
by cogier
Here is my take on this. You can edit Columns 0 & 3 but not 1. The sort is probably not recommended, but it works for me.

Image
TableEdit-0.0.1.tar.gz
(12.38 KiB) Downloaded 268 times

Re: TableView edit : 'freeze' column(s) ?

Posted: Thursday 11th March 2021 8:37am
by Doctor Watson
Hi guys,

01McAc : That ‘freeze’ code works perfectly.
As for the sorting routine, I had just found the code in the GridView documentation, and where it says that it can be applied to TableView as well. However, it’s well above my programming knowledge.
So I’m very pleased with Cogier’s sollution. That one I can handle.
Recommended or not, the end justifies the means. I hope I can take it from here.
Thanks a lot.