Grid with Combo Box from Table

You can share your experience with HMG. Share with some screenshots/project details so that others will also be benefited.

Moderator: Rathinagiri

Post Reply
User avatar
sudip
Posts: 1443
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Grid with Combo Box from Table

Post by sudip » Sun Mar 15, 2009 1:00 pm

Hi,

I just created an example of Grid with Combobox from tables. I used 3 arrays for Grid, Source, Display.
I just wrote data retrieve part. Code for saving data to tables can be easily written.
I used some code snippets of one of Grid examples from HMG Sampes.
(Thank you Rathi, for tips about Grid and Combobox)
Please tell me where I can improve the code.

#include "minigui.ch"

Function Main
local aGrid := {}, aSource := {}, aDisp := {}, i

CreateTable()

select item
do while !eof()
aadd(aSource, itemcd)
aadd(aDisp, itemnm)
skip
enddo

select stock
do while !eof()
i = ascan(aSource, {|x| x == stock->itemcd})
aadd(agrid, {i, qty})
skip
enddo


DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 640 ;
HEIGHT 400 ;
TITLE 'Grid with Combo Box from Table' ;
MAIN

DEFINE MAIN MENU
DEFINE POPUP 'File'
MENUITEM 'Get Item' ACTION GetItem()
END POPUP
END MENU

@ 10,10 GRID Grid_1 ;
WIDTH 620 ;
HEIGHT 330 ;
HEADERS {'Item','Qty'} ;
WIDTHS {140,140} ;
ITEMS aGrid ;
EDIT ;
COLUMNCONTROLS { {'COMBOBOX', aDisp}, {'TEXTBOX', 'NUMERIC', '999999'}} ;

END WINDOW

CENTER WINDOW Form_1

ACTIVATE WINDOW Form_1

Return


PROCEDURE GETITEM()
local a

a := Form_1.Grid_1.Item (2)

msginfo("Col 1 =>"+str(a[1])+" Col 2 =>"+str(a[2]))

RETURN



FUNCTION CreateTable()
local aDbf := {}

if !file("item.dbf")
aDbf := {}
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"itemnm", "c", 20, 0})
dbcreate("item", adbf)
use item new

select item

append blank
/* as the table name is "item", I can't write codes like item->itemcd := "COMP" ;( it gives error */
replace itemcd with "COMP"
replace itemnm with "Computer"

append blank
replace itemcd with "CD"
replace itemnm with "Compact Disk"

append blank
replace itemcd with "OPM"
replace itemnm with "Optical Mouse"

use
endif

if !file("stock.dbf")
aDbf := {}
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"qty", "n", 6, 0})
dbcreate("stock", adbf)
use stock new

select stock

append blank
stock->itemcd := "OPM"
stock->qty := 100

append blank
stock->itemcd := "COMP"
stock->qty := 20

append blank
stock->itemcd := "CD"
stock->qty := 5000

use
endif

close databases
use item new
use stock new

return nil


Regards.

Sudip
With best regards,
Sudip

User avatar
sudip
Posts: 1443
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Post by sudip » Sun Mar 15, 2009 3:47 pm

Hi,

I added SAVE and DELETE features in Grid.

Please check this:

#include "minigui.ch"

Function Main
local aGrid := {}, aSource := {}, aDisp := {}, i

CreateTable()

select item
do while !eof()
aadd(aSource, itemcd)
aadd(aDisp, itemnm)
skip
enddo

select stock
do while !eof()
i = ascan(aSource, {|x| x == stock->itemcd})
aadd(agrid, {i, qty})
skip
enddo


DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 640 ;
HEIGHT 400 ;
TITLE "Grid with Combobox from Table" ;
MAIN

DEFINE MAIN MENU
DEFINE POPUP 'File'
MENUITEM 'Get Item' ACTION GetItem()
MENUITEM 'Delete Item' ACTION DelItem()
MENUITEM 'Save Data' ACTION SaveData(aSource)
END POPUP
END MENU

@ 10,10 GRID Grid_1 ;
WIDTH 620 ;
HEIGHT 330 ;
HEADERS {'Item','Qty'} ;
WIDTHS {140,140} ;
ITEMS aGrid ;
EDIT ;
COLUMNCONTROLS { {'COMBOBOX', aDisp}, {'TEXTBOX', 'NUMERIC', '999999'}} ;

END WINDOW

CENTER WINDOW Form_1

ACTIVATE WINDOW Form_1

Return



PROCEDURE DelItem()
if msgyesno("Delete Item", "Grid with Combobox from Table")
Form_1.Grid_1.DeleteItem(Form_1.Grid_1.Value)
endif
return



PROCEDURE SaveData(aSource)
local i, aTemp := {}

select stock
zap
for i = 1 to Form_1.Grid_1.Itemcount
aTemp := Form_1.Grid_1.Item(i)
append blank
if aTemp[1] >= 1 .and. aTemp[1] <= len(aSource)
stock->itemcd := aSource[aTemp[1]]
endif
stock->qty := aTemp[2]
next
return nil



PROCEDURE GETITEM()
local a

a := Form_1.Grid_1.Item (2)

msginfo("Col 1 =>"+str(a[1])+" Col 2 =>"+str(a[2]))

RETURN



FUNCTION CreateTable()
local aDbf := {}

if !file("item.dbf")
aDbf := {}
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"itemnm", "c", 20, 0})
dbcreate("item", adbf)
use item new

select item

append blank
replace itemcd with "COMP"
replace itemnm with "Computer"

append blank
replace itemcd with "CD"
replace itemnm with "Compact Disk"

append blank
replace itemcd with "OPM"
replace itemnm with "Optical Mouse"

use
endif

if !file("stock.dbf")
aDbf := {}
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"qty", "n", 6, 0})
dbcreate("stock", adbf)
use stock new

select stock

append blank
stock->itemcd := "OPM"
stock->qty := 100

append blank
stock->itemcd := "COMP"
stock->qty := 20

append blank
stock->itemcd := "CD"
stock->qty := 5000

use
endif

close databases
use item new
use stock new

return nil


Regards.

Sudip
With best regards,
Sudip

User avatar
Rathinagiri
Posts: 5096
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 93 times
Been thanked: 107 times
Contact:

Post by Rathinagiri » Sun Mar 15, 2009 5:07 pm

Hi,

Can you please attach as a HMG project with sample dbf files too to enable us to run the project?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
esgici
Posts: 4282
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 166 times
Been thanked: 61 times
Contact:

Post by esgici » Sun Mar 15, 2009 5:45 pm

Hi Sudip

Good work, thanks for sharing :)
sudip wrote: ... where I can improve the code?
Improvement is a relative and endless concept. While someones said "if works, don't touch it!" someones said "how I can arrive to the best?"

IMHO in programming there is never best; because always there is a better.

No improvement, only my two cents :

Before change

LOCAL aGrid := {}, aSource := {}, aDisp := {}, i

to PRIVATE.

And than your GetItem() procedure :

Code: Select all

PROCEDURE GETITEM()

   LOCAL nCurRow, nItemNo 
   
   IF Form_1.Grid_1.Value < 1
      Form_1.Grid_1.Value := 1
   ENDIF
      
   nCurRow := Form_1.Grid_1.Value
   nItemNo := Form_1.Grid_1.Cell( nCurRow, 1 )

   MsgInfo( "Item No : "   + STR( nItemNo, 3 ) + CRLF + ;
            "Item Code : " + aSource[ nItemNo ] + CRLF + ;
            "Item Name : " + aDisp[ nItemNo ] + CRLF + ;
            "Quantity : "  + STR( Form_1.Grid_1.Cell( nCurRow, 2 ), 7 ) ) 

RETURN
Happy HMG'ing :D

Regards

--

Esgici
Viva INTERNATIONAL HMG :D

User avatar
sudip
Posts: 1443
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Post by sudip » Mon Mar 16, 2009 3:26 am

Hi Rathi,

The program file, I sent, will automatically create dbf files. I compiled with compile.bat file. If you need .hpj file I shall create a project and send you.

Thank you very much for your interest.

With best regards.

Sudip
With best regards,
Sudip

User avatar
sudip
Posts: 1443
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Post by sudip » Mon Mar 16, 2009 3:32 am

Hi Esgici,

Thank you very much for your comments and encouragement. :) I really need it. And they give me a mental boost up for HMGing :)

I incorporated all your advice into the program.

Thank you all.

With best regards.

Sudip
With best regards,
Sudip

User avatar
Rathinagiri
Posts: 5096
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 93 times
Been thanked: 107 times
Contact:

Post by Rathinagiri » Mon Mar 16, 2009 4:15 am

I had checked your code and it works like expected.

Thanks for sharing Sudip.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests