How to add a new row in my datagrid and remove as well

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 12:58 pm

Hi friends.

I have a datagrid that involves cars, year model, kilometer and price.

I'm trying to make two buttons, one that adds cars to the grid and one that removes it.

BUT, when I remove one of the rows I also want that to be stored in some sort of field that shows that the car has been sold. So when the row is removed, it basically shows that it's been sold on another field.

I want a button that when I click on it, it will add another row asking for what car it is, what year model, kilometers it driven and price.

This is what I have for now:

https://prnt.sc/2yYGLUlmgltW

Thanks in advance

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 1:38 pm

Hi Curiousbystander,

1. add data
Ask your user what he/she want to add and store everything into a variable.
Then put this info into one TAB delimited line:

Code: Select all

...
put tMerke & TAB & tModell & TAB & tKM & TAB & tPris into tNewLineData
## Here the command to add data to a datagrid of type TABLE:
dispatch "AddLine" to group "Your datagrid here" with tNewLineData

## If you do not supply the second and third parameter tColumns and tLine then
## LC will append a new line at the end of your datagrid.
...
2. Where do you want to store the info about sold cars?
We need to know this before we can proceed.

Howeer you can extract the data the user wants to delete first with:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "Your datagrid here" into tLine

## Now fetch the DATA in this line
put the dgDataOfLine[tLine] of grp "Your datagrid here" into tData

## Set itemdel to TAB
put item 1 of tData into tMerke
put item 2 of tData into tModell
## etc...
## So you can store whatever you need from these infos later...
...
Best

Klaus

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 1:43 pm

Klaus wrote:
Mon Oct 17, 2022 1:38 pm
Hi Curiousbystander,

1. add data
Ask your user what he/she want to add and store everything into a variable.
Then put this info into one TAB delimited line:

Code: Select all

...
put tMerke & TAB & tModell & TAB & tKM & TAB & tPris into tNewLineData
## Here the command to add data to a datagrid of type TABLE:
dispatch "AddLine" to group "Your datagrid here" with tNewLineData

## If you do not supply the second and third parameter tColumns and tLine then
## LC will append a new line at the end of your datagrid.
...
2. Where do you want to store the info about sold cars?
We need to know this before we can proceed.

Howeer you can extract the data the user wants to delete first with:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "Your datagrid here" into tLine

## Now fetch the DATA in this line
put the dgDataOfLine[tLine] of grp "Your datagrid here" into tData

## Set itemdel to TAB
put item 1 of tData into tMerke
put item 2 of tData into tModell
## etc...
## So you can store whatever you need from these infos later...
...
Best

Klaus
...

I was just thinking storing the info on the sold cars on like next card right, just like a list of what is sold. Go to the next card, then the removed ones are on a list that shows what cars are sold.

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 1:45 pm

OK, then just add the infos to that field:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "Your datagrid here" into tLine

## Now fetch the DATA in this line
put the dgDataOfLine[tLine] of grp "Your datagrid here" into tData

## Set itemdel to TAB
put item 1 of tData into tMerke
put item 2 of tData into tModell
## etc...
put CR & tMerke after fld "sold cars" of cd "the card with that field of sold cars"
...
You get the picture...

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 2:05 pm

Klaus wrote:
Mon Oct 17, 2022 1:45 pm
OK, then just add the infos to that field:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "Your datagrid here" into tLine

## Now fetch the DATA in this line
put the dgDataOfLine[tLine] of grp "Your datagrid here" into tData

## Set itemdel to TAB
put item 1 of tData into tMerke
put item 2 of tData into tModell
## etc...
put CR & tMerke after fld "sold cars" of cd "the card with that field of sold cars"
...
You get the picture...
Can we go back to the first step real quick. Add data part, so like. I have the button, I'll click on it and it should ask what model, km, price, etc. How exactly do I ask and put the answers into the grid. It's been a while since I've done any livecode.

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 2:22 pm

You quoted the part that will add the deleted = sold cars!
You mean just a couple of ASK dialogs and storing the users answer in variables:

Code: Select all

...
## Ask user to enter merk, modell etc...
ask "Merke?"

## User clicked CANCEL:
if the result = "cancel" then
  exit to top
end if
put it into tMerke

ask "Modell?"
if the result = "cancel" then
  exit to top
end if
put it into tModell

ask "KM?"
if the result = "cancel" then
  exit to top
end if
put it into tKM
ask "Pris?"
if the result = "cancel" then
  exit to top
end if
put it into tPris
## Now you have all neccessary infos and you can store it wherever you want to.
...
Now where exactly do you want to store this info about sold cars?
Filed? Another datagrid? On the same card? On card XXX?

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 2:30 pm

Klaus wrote:
Mon Oct 17, 2022 2:22 pm
You quoted the part that will add the deleted = sold cars!
You mean just a couple of ASK dialogs and storing the users answer in variables:

Code: Select all

...
## Ask user to enter merk, modell etc...
ask "Merke?"

## User clicked CANCEL:
if the result = "cancel" then
  exit to top
end if
put it into tMerke

ask "Modell?"
if the result = "cancel" then
  exit to top
end if
put it into tModell

ask "KM?"
if the result = "cancel" then
  exit to top
end if
put it into tKM
ask "Pris?"
if the result = "cancel" then
  exit to top
end if
put it into tPris
## Now you have all neccessary infos and you can store it wherever you want to.
...
Now where exactly do you want to store this info about sold cars?
Filed? Another datagrid? On the same card? On card XXX?

Same card, and I suppose a grid again but only with the sold ones yeah.

OH also, how exactly would I use functions JsonImport( ) and JsonExport( ) to convert JSON to array and array to JSON

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 2:46 pm

OK, I already gave half the answer:

Code: Select all

...
## See above for the rest of the script...
...
put it into tPris

## Now you have all neccessary infos and you can store it wherever you want to.
put tMerke & TAB & tModell & TAB & tKM & TAB & tPris into tNewLineData

## Here the command to add data to a datagrid of type TABLE:
dispatch "AddLine" to group "Your other datagrid with the sold cars" with tNewLineData
...

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 2:50 pm

Curiousbystander wrote:
Mon Oct 17, 2022 2:30 pm
...
Oh also, how exactly would I use functions JsonImport( ) and JsonExport( ) to convert JSON to array and array to JSON
To convert a complete JSON array to LC array and back:

Code: Select all

...
## JSON -> Array
put JSONtoArray(the_raw_json_array) into tLCArray
...
## Array -> JSON
put JSONtoArray(tLCArray) into tJSONArray
...

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 3:48 pm

Ok! I'm kind of stuck now.

So I click delete. And this happens: https://prnt.sc/mhAPNSvxxXB1

I want it to remove it from the top grid and add it onto the second grid under it. Which is sold cars.

Need a little help.

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 3:59 pm

Hm, works here in my quick and dirty test:
Bildschirmfoto 2022-10-17 um 16.56.00.png
?

If you like you can send me the stack and I will take a look.
-> klaus AT major-k.de

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 4:02 pm

To actually delete a line use -> DeleteLine

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "datagrid" into tLineNumber

## Delete that line from datagrid:
dispatch "deleteLine" to grp "datagrid" with tLineNumber
...

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 4:31 pm

Klaus wrote:
Mon Oct 17, 2022 4:02 pm
To actually delete a line use -> DeleteLine

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "datagrid" into tLineNumber

## Delete that line from datagrid:
dispatch "deleteLine" to grp "datagrid" with tLineNumber
...
I sent you an email, my friend. Hopefully you can help me a little. Thank you again for your help so far

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to add a new row in my datagrid and remove as well

Post by Klaus » Mon Oct 17, 2022 4:43 pm

This script already shows the solution:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "datagrid" into tLineNumber

## Delete that line from datagrid:
dispatch "deleteLine" to grp "datagrid" with tLineNumber
...
In your script "on slettBil" you forget the first part: finding out the currently selected line!
DELETELINE expects a NUMBER but you supplied a string, and we can see the strange result.

Please try to understand what we suggest (see above) and ASK if something is not clear,
otherwise you are wasting our time, we are all volunteers here!

Copy/paste without thinkíng is never a good idea, especially in software programming.

Curiousbystander
Posts: 20
Joined: Wed Sep 07, 2022 4:26 pm

Re: How to add a new row in my datagrid and remove as well

Post by Curiousbystander » Mon Oct 17, 2022 5:20 pm

Klaus wrote:
Mon Oct 17, 2022 4:43 pm
This script already shows the solution:

Code: Select all

...
## What line is selected:
put the dghilitedline of grp "datagrid" into tLineNumber

## Delete that line from datagrid:
dispatch "deleteLine" to grp "datagrid" with tLineNumber
...
In your script "on slettBil" you forget the first part: finding out the currently selected line!
DELETELINE expects a NUMBER but you supplied a string, and we can see the strange result.

Please try to understand what we suggest (see above) and ASK if something is not clear,
otherwise you are wasting our time, we are all volunteers here!

Copy/paste without thinkíng is never a good idea, especially in software programming.
Ok, I fixed the deleting part. But when I delete it right, how do I send the deletion to the "sold car grid", the 2nd grid under it. How do I get it that when I remove it up there it goes to the other grid below.

Post Reply