TableView edit : 'freeze' column(s) ?

Post

Posted
Rating:
#1 (In Topic #614)
Regular
Doctor Watson is in the usergroup ‘Regular’
 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.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
01McAc is in the usergroup ‘Regular’
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 k17:k17.8:start [GAMBAS BOOK 3.19.5]
I set column 1 of the tableview to readonly by cancelling the event. Have a look at the code.

Code (gambas)

  1. Public Sub TableView1_Click()
  2.    'If cbxEditModus.Value = False Then
  3.    '   TableView1.Edit()
  4.    'Else
  5.       Select Case TableView1.Column
  6.         Case 0
  7.           TableView1.Edit(["Linux", "Windows", "OS-X"])
  8.         Case 1
  9.           Stop Event  'Event needs to be stopped and a Return is necessary
  10.           Return
  11.         Case 2
  12.           TableView1.Edit(["Desktop-PC", "NoteBook", "NetBook", "Tablet PC"], True)
  13.       End Select
  14.    'Endif ' cbxEditModus.Value = False ?
  15. End ' TableView1_Click()

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
01McAc is in the usergroup ‘Regular’
WRT Sorting: just turn on the sorting in the properties.

Code

Tableview1.Sort=true
You need to add your own sort algorithm into the sort event

Code (gambas)

  1. Public Sub TableView1_Sort()
  2.  
  3.   Dim Values, ValueSorted As New String[]
  4.   Dim Nx, iNx As Integer
  5.  
  6.   ' Load sort column into string array
  7.   For Nx = 0 To TableView1.Rows.Max
  8.     Values.Add(TableView1[Nx, TableView1.Columns.Sort].Text)
  9.   Next
  10.  
  11.   ' Copy values to new array and sort it based on status of sort indicator
  12.   ValueSorted = Values.Copy()
  13.   ValueSorted.Sort(IIf(TableView1.Columns.Ascending, gb.Ascent, gb.Descent))
  14.  
  15.   ' Iterate through table swapping appropriate values
  16.   For Nx = 0 To ValueSorted.Max
  17.     For iNx = 0 To TableView1.Columns.Max
  18.       Swap TableView1[Nx, iNx].Text, TableView1[Values.Find(ValueSorted[Nx], 0, Nx), iNx].Text
  19.     Next
  20.  
  21.     ' Pick up new order to preserve proper handling of indexing and duplicate values
  22.     Values.Clear()
  23.     For iNx = 0 To TableView1.Rows.Max
  24.       Values.Add(TableView1[iNx, TableView1.Columns.Sort].Text)
  25.     Next
  26.  
  27.   Next
  28.  
  29.   TableView1.Refresh()
  30.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Looking at the wiki page /comp/gb.form/tableview - Gambas Documentation

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 said

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
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

<IMG src="https://www.cogier.com/gambas/TableEdit.png"> </IMG>

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
Doctor Watson is in the usergroup ‘Regular’
 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.

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top
1 guest and 0 members have just viewed this.