Grid with Combo Box from Table
Posted: 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
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