Page 1 of 1

Grid with Combo Box from Table

Posted: Sun Mar 15, 2009 1:00 pm
by sudip
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

Re: Grid with Combo Box from Table with SAVE and DELETE

Posted: Sun Mar 15, 2009 3:47 pm
by sudip
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

Re: Grid with Combo Box from Table

Posted: Sun Mar 15, 2009 5:07 pm
by Rathinagiri
Hi,

Can you please attach as a HMG project with sample dbf files too to enable us to run the project?

Re: Grid with Combo Box from Table

Posted: Sun Mar 15, 2009 5:45 pm
by esgici
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

Re: Grid with Combo Box from Table

Posted: Mon Mar 16, 2009 3:26 am
by sudip
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

Re: Grid with Combo Box from Table

Posted: Mon Mar 16, 2009 3:32 am
by sudip
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

Re: Grid with Combo Box from Table

Posted: Mon Mar 16, 2009 4:15 am
by Rathinagiri
I had checked your code and it works like expected.

Thanks for sharing Sudip.