views:

397

answers:

2
+2  Q: 

Wxwidget Grid

Hi. I posted this in the mailing list, but the reply I got wasn't too clear, so maybe I'll have better luck here.

I currently have a grid with data in it. I would like to know if there is a way to give each generated row an ID, or at least, associate each row with an object.

It may make it more clear if I clarify what i'm doing. It is described below.

I pull data from an SQL table and display them in the grid. I am allowing for the user to add/delete rows and edit cells.

Say the user is viewing a grid that has 3 rows(which is, in turn, a mysql table with 3 rows). If he is on the last row and presses the down arrow key, a new row is created and he can enter data into it and it will be inserted in the database when he presses enter.

However, I need a way to find out which rows will use "insert" query and which will use "update" query.

So ideally, when the user creates a new row by pressing the down arrow, I would give that row an ID and store it in a list(or, if rows already have IDs, just store it in a list) and when the user finishes entering data in the cells and presses enter, I would check if that row's ID is in the in the list. If it is, i would insert all of that row's cells values into the table, if not, i would update mysql with the values.

Hope I made this clear.

+3  A: 

What I did when I encountered such a case was to create a column for IDs and set its width to 0.

Alan Haggai Alavi
Ah, clever! However, what if i need to store more info then just and iD? It could be cumbersome. Will resort to this if all else fails though.
lyrae
an ID is going to be plenty, use it as a key into a separate dictionary if you need to associate lots of info with each row!
Alex Martelli
+2  A: 

You could make your own GridTableBase that implements this, for a simple example to get you started see my answer to this question.

Toni Ruža
Thank you. Will give it a try.
lyrae
I don't see how to accomplish this. I looked through all methods of GridTableBase and don't see how I can link a give a row an ID or link a row to an object/dict/list.
lyrae
By using GridTableBase you effectively bind your custom data structure to a Grid. Your custom data structure can implement all the extra functionality you need. For example, the GetValue and SetValue methods give you the row in the grid and your custom data structure can map those rows to IDs.
Toni Ruža
I have learned how to use the pygridtablebase. And I connect the grid to my data on pygridtablebase. However, mapping a row to an ID/object is where i'm having trouble. I can't rely on getting a row's position because previous rows may be deleted. So what am i doing now, is using a pygridtablebase, but also Alan's method of storing an ID in a column who's width is 0
lyrae
You can intercept those deletions as well as any other action preformed on your data in the grid, see DeleteRows method in GridTableBase. If you would, for example, implement your data structure as a list-like object you could use the row index you get directly to index that list. The deletions you talk about would not be an issue because the grid and your data would always be in sync.
Toni Ruža