本帖最后由 jycel 于 2009-9-18 10:32 编辑
最近在学习listview和数据库相关操作
如图所示,要么能排序但无法使用网格,使用了网格又无法排,而且照论坛上access操作例子来,后面还会自生一个点
不使用自动读取就正常
在读取的时候,就会在数据后面加很长的空字符!造成数据错误如图三的商品名称
#NoTrayIcon
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ButtonConstants.au3>
#include <Access.au3>
$adSource="jycel.mdb"
$mdb_data_pwd="" ;数据库密码
$suminame=""
$T="*" ;列表
$adTable="Goods" ;数据表
Dim $ListView2
If Not FileExists("jycel.mdb") Then
data($adSource, $adTable)
EndIf
_spgl()
Func _spgl()
$Form2 = GUICreate("商品管理", 541, 374);, 273, 218)
$Group1 = GUICtrlCreateGroup("基本功能:", 6, 0, 529, 49)
$Button5 = GUICtrlCreateButton("新增", 14, 16, 81, 25, $WS_GROUP)
$Button6 = GUICtrlCreateButton("修改", 158, 16, 81, 25, $WS_GROUP)
$Button7 = GUICtrlCreateButton("保存", 302, 16, 81, 25, $WS_GROUP)
$Button8 = GUICtrlCreateButton("读取", 446, 16, 81, 25, $WS_GROUP)
$Dummy = GUICtrlCreateDummy()
$Context = GUICtrlCreateContextMenu($Dummy)
$all = GUICtrlCreateMenuItem("查询全部", $Context)
GUICtrlCreateMenuItem("", $Context)
$yl = GUICtrlCreateMenuItem("查询饮料", $Context)
$xy = GUICtrlCreateMenuItem("查询香烟", $Context)
$dk = GUICtrlCreateMenuItem("查询点卡", $Context)
$sw = GUICtrlCreateMenuItem("查询食物", $Context)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("商品信息:", 6, 56, 529, 313)
$ListView2 = GUICtrlCreateListView("ID|商品名称|商品单价|商品分类|商品库存", 14, 72, 513, 289, BitOR($LVS_REPORT,$LVS_EDITLABELS,$LVS_NOSORTHEADER,$LVS_SORTASCENDING,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_SORTASCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
;$ListView2 = GUICtrlCreateListView("ID|商品名称|商品单价|商品分类|商品库存", 14, 72, 513, 289);, BitOR($LVS_REPORT,$LVS_EDITLABELS,$LVS_NOSORTHEADER,$LVS_SINGLESEL,$LVS_SHOWSELALWAYS,$LVS_SORTASCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
;,$LVS_REPORT,BitOR($LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 120)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 118)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 120)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 100)
$menu = GUICtrlCreateContextMenu($ListView2)
$add_menu = GUICtrlCreateMenuItem("添加商品数据", $menu)
$e_menu = GUICtrlCreateMenuItem("编辑选中项目", $menu)
$d_menu = GUICtrlCreateMenuItem("删除选中项目", $menu)
$c_menu = GUICtrlCreateMenuItem("清空所有数据", $menu)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;du()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button5
_newsp()
du()
Case $Button6
$Listv2 = _GUICtrlListView_GetSelectedIndices($ListView2)
If Not StringLen($Listv2) Then
MsgBox(32,"提示","请选择后再进行修改操作!")
Else
_xgsp()
EndIf
Case $Button7 ;写入数据库
MsgBox(0,"","正在开发中……")
Case $Button8
du()
Case $add_menu
_newsp()
case $e_menu
_xgsp()
case $d_menu
$Index1 = _GUICtrlListView_GetSelectedIndices($ListView2)
If Not StringLen($Index1) Then
MsgBox(32,"提示","请选择后再进行删除操作!")
Else
del()
du()
EndIf
case $c_menu
$List_n=_GUICtrlListView_GetItemCount($ListView2)
If Not $List_n Then
MsgBox(32,"提示","当前列表无任何数据!")
Else
_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView2))
MsgBox(32,"提示","数据清空成功!")
EndIf
Case $all
MsgBox(64,"查询全部","你点的是查询全部")
EndSwitch
WEnd
EndFunc
Func _newsp();新增
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\新增商品.kxf
Dim $Form2
$Form3 = GUICreate("新增商品", 196, 183)
$Group1 = GUICtrlCreateGroup("新增商品", 0, 0, 193, 177)
$Label6 = GUICtrlCreateLabel("商品名称:", 8, 20, 64, 17)
$Input1 = GUICtrlCreateInput("", 80, 16, 100, 21)
$Label7 = GUICtrlCreateLabel("商品单价:", 8, 52, 64, 17)
$Input2 = GUICtrlCreateInput("", 80, 48, 100, 21)
$Label8 = GUICtrlCreateLabel("商品分类:", 8, 84, 64, 17)
$Combo1 = GUICtrlCreateCombo("", 80, 80, 100, 25)
GUICtrlSetData(-1,"饮料|香烟|点卡|食物","饮料")
$Label9 = GUICtrlCreateLabel("商品库存:", 8, 116, 64, 17)
$Input3 = GUICtrlCreateInput("", 80, 112, 84, 21)
$Label10 = GUICtrlCreateLabel("件", 168, 116, 16, 17)
$Button9 = GUICtrlCreateButton("增加", 8, 144, 50, 20, $WS_GROUP)
$Button10 = GUICtrlCreateButton("返回", 128, 144, 50, 20, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form3)
GUISetState(@SW_SHOW,$Form2)
GUISetState(@SW_ENABLE, $Form2)
ExitLoop
Case $Button9
Dim $n=0
$spmc=GUICtrlRead($Input1)
$spdj=GUICtrlRead($Input2)
$spfl=GUICtrlRead($Combo1)
$spkc=GUICtrlRead($Input3)
_accessAddRecord($adSource, $adTable, $spmc&"|"&$spdj&"|"&$spfl&"|"&$spkc,1)
du()
Case $Button10
GUIDelete($Form3)
GUISetState(@SW_SHOW,$Form2)
GUISetState(@SW_ENABLE, $Form2)
ExitLoop
EndSwitch
WEnd
EndFunc
Func _xgsp();修改
#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\桌面\新增商品.kxf
Dim $Form2
$Listv2 = _GUICtrlListView_GetSelectedIndices($ListView2)
If Not StringLen($Listv2) Then
MsgBox(32,"提示","请选择后再进行修改操作!")
EndIf
$spmc1=_GUICtrlListView_GetItemText($ListView2, Number($Listv2),1)
$spdj1=_GUICtrlListView_GetItemText($ListView2, Number($Listv2),2)
$spfl1=_GUICtrlListView_GetItemText($ListView2, Number($Listv2),3)
$spkc1=_GUICtrlListView_GetItemText($ListView2, Number($Listv2),4)
$Form4 = GUICreate("修改商品", 196, 183)
$Group1 = GUICtrlCreateGroup("修改商品", 0, 0, 193, 177)
$Label6 = GUICtrlCreateLabel("商品名称:", 8, 20, 64, 17)
$Input4 = GUICtrlCreateInput($spmc1, 80, 16, 100, 21)
$Label7 = GUICtrlCreateLabel("商品单价:", 8, 52, 64, 17)
$Input5 = GUICtrlCreateInput($spdj1, 80, 48, 100, 21)
$Label8 = GUICtrlCreateLabel("商品分类:", 8, 84, 64, 17)
$Combo2 = GUICtrlCreateCombo($spfl1, 80, 80, 100, 25)
GUICtrlSetData(-1,"饮料|香烟|点卡|食物","饮料")
$Label9 = GUICtrlCreateLabel("商品库存:", 8, 116, 64, 17)
$Input6 = GUICtrlCreateInput($spkc1, 80, 112, 84, 21)
$Label10 = GUICtrlCreateLabel("件", 168, 116, 16, 17)
$Button11 = GUICtrlCreateButton("修改", 8, 144, 50, 20, $WS_GROUP)
$Button12 = GUICtrlCreateButton("返回", 128, 144, 50, 20, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form4)
GUISetState(@SW_SHOW,$Form2)
GUISetState(@SW_ENABLE, $Form2)
ExitLoop
Case $Button11
$spmc=GUICtrlRead($Input4)
$spdj=GUICtrlRead($Input5)
$spfl=GUICtrlRead($Combo2)
$spkc=GUICtrlRead($Input6)
$Strn=GUICtrlRead(GUICtrlRead($ListView2))
$Strnspin=StringSplit($Strn,"|")
_accessUpdateRecord($adSource,$adTable,"ID",$Strnspin[1],"商品名称",$spmc)
_accessUpdateRecord($adSource,$adTable,"ID",$Strnspin[1],"商品单价",$spdj)
_accessUpdateRecord($adSource,$adTable,"ID",$Strnspin[1],"商品分类",$spfl)
_accessUpdateRecord($adSource,$adTable,"ID",$Strnspin[1],"商品库存",$spkc)
du()
MsgBox(4096, "提示:",$Strnspin[1]&" 更新成功!!!")
Case $Button12
GUIDelete($Form4)
GUISetState(@SW_SHOW,$Form2)
GUISetState(@SW_ENABLE, $Form2)
ExitLoop
EndSwitch
WEnd
EndFunc
Func du()
GUICtrlSendMsg($ListView2, $LVM_DELETEALLITEMS, 0, 0)
$oADO = _dbOpen($adSource)
$RS = _dbOpenRecordset();ObjCreate("ADODB.Recordset")
$RS.Open ("SELECT * FROM " & $adTable , $oADO, $adOpenStatic, $adLockOptimistic)
while Not $RS.eof And Not $RS.bof
if @error =1 Then ExitLoop
GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value&"|"& $RS.Fields (3).value&"|"& $RS.Fields (4).value,$ListView2)
; MsgBox(0, "", $RS.Fields (1).value);显示表第一个数据
$rs.movenext
WEnd
$rs.close
$oADO.Close
EndFunc
Func del()
$Strn=GUICtrlRead(GUICtrlRead($ListView2))
$Strnspin=StringSplit($Strn,"|")
_accessDeleteRecord($adSource,$adTable, "ID",$Strnspin[1],1)
MsgBox(4096, "提示:",$Strnspin[1]&" 删除成功!!!",1)
EndFunc
|