udbghdto 发表于 2011-8-9 19:42:28

怎么读取LISTVIEW指定字段。算出总数[已解决]

本帖最后由 udbghdto 于 2011-8-10 10:26 编辑

怎么读取LISTVIEW指定字段。算出总数。。不是数据库字段。

LISTVIEW数值从数据库读取。然后读取LISTVIEW指定字段算出总价。
因为要用到查询功能。LISTVIEW数量不固定。就不能直接数据库读取。

数据库文件。
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$mdb = @ScriptDir & "\db1.mdb"
$Table = "db1"
$T = "*"

$Form1_1 = GUICreate("Form1", 880, 442, 183, 114)
$ListView1 = GUICtrlCreateListView("编号 |品名|类型|数量|单价|总价|经办人|单位或个人|货款|日期", 56, 24, 673, 297)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 5, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 6, 70)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 7, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 8, 60)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 9, 50)
$Label1 = GUICtrlCreateLabel("合计:元", 64, 344, 84, 28)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

dumdb()
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd
Func dumdb()
        GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
        $addfld = ObjCreate("ADODB.Connection")
        $addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb)
        $RS = ObjCreate("ADODB.Recordset")
        $RS.ActiveConnection = $addfld
        $RS.Open("Select " & $T & " From " & $Table)
        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 & "|" & $RS.Fields(5).value & "|" & $RS.Fields(6).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(8).value & "|" & $RS.Fields(9).value, $ListView1)
                $RS.movenext
        WEnd
        $RS.close
        $addfld.Close
       
       
       
EndFunc   ;==>dumdb

ceoguang 发表于 2011-8-10 07:52:57


#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
$mdb = @ScriptDir & "\db1.mdb"
$Table = "db1"
$T = "*"

$Form1_1 = GUICreate("Form1", 880, 442, 183, 114)
$ListView1 = GUICtrlCreateListView("编号 |品名|类型|数量|单价|总价|经办人|单位或个人|货款|日期", 56, 24, 673, 297)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 80)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 5, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 6, 70)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 7, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 8, 60)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 9, 50)
$Label1 = GUICtrlCreateLabel("合计:元", 64, 344, 120, 28)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

dumdb()
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd
Func dumdb()
        GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
        $addfld = ObjCreate("ADODB.Connection")
        $addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb)
        $RS = ObjCreate("ADODB.Recordset")
        $RS.ActiveConnection = $addfld
        $RS.Open("Select " & $T & " From " & $Table)
        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 & "|" & $RS.Fields(5).value & "|" & $RS.Fields(6).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(8).value & "|" & $RS.Fields(9).value, $ListView1)
                $RS.movenext
        WEnd
        $RS.close
        $addfld.Close
        Local $iTotal = 0
        For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1
                $iTotal += _GUICtrlListView_GetItemText($ListView1, $i, 4)
        Next
        GUICtrlSetData($Label1, '合计:' & $iTotal & ' 元')
EndFunc   ;==>dumdb

xyhqqaa 发表于 2011-8-10 08:17:10

谢谢分享。。。下次学习会用到,,留个记号

lxz 发表于 2011-8-10 08:28:17

代码收下,谢谢...

lxz 发表于 2011-8-10 08:30:57

好象代码出错吧?

udbghdto 发表于 2011-8-10 11:18:17

回复 6# ceoguang


      Local $iTotal = 0


      For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1


                $iTotal += _GUICtrlListView_GetItemText($ListView1, $i, 4)


      Next

能不能注解一下呀。
页: [1]
查看完整版本: 怎么读取LISTVIEW指定字段。算出总数[已解决]