首页 > 编程 > .NET > 正文

VB.NET中LISTVIEW排序

2024-07-10 13:07:11
字体:
来源:转载
供稿:网友


控件:

textbox    :1个       name:txtjobno

button:     2个       name:btnfilter,btnshowall

listview :  1个       name:lstvwjobno  columns:jobno,containerid,cartonid,po,style,color,size,shipment,factory             sorting:none


源代码:

imports system.data.sqlclient

dim con as new sqlconnection
dim varsql as string
dim r as integer
dim constring as string ="data source=127.0.0.1;initial catalog=mydb; uid=sa;password=123"


'定义默认排序变量
dim mysort as string = "asc"

'定义默认字段变量
dim myfield as string = "job.jobno"

'过滤指定条件的记录
    private sub btnfilter_click(byval sender as system.object, byval e as system.eventargs) handles btnfilter.click
        try
            dim fjobno as string = ""
            fjobno = txtjobno.text.trim()
            r = list_dgjobno(fjobno)

        catch ex as exception
            messagebox.show(ex.tostring)
        finally
            if con.state = connectionstate.open then
                con.close()
            end if
        end try
    end sub


'显示所有记录
private sub btnshowall_click(byval sender as system.object, byval e as system.eventargs) handles btnshowall.click
        list_dgjobno("")
end sub


'函数功能:将读取出来的数据写入lstvwjovno的表格中
'函数名称:list_dgjobno,调用格式:list_dgjobno("10022"),list_dgjobno("")
'参    数:fjobno,要查询的工作号jobno,为空时则查询所有的
'编 写 者:sd
'日    期:2005-9-23
'转载请保留此信息
private function list_dgjobno(byval fjobno as string) as integer
        try
            cursor.current = cursors.waitcursor

            lstvwjobno.items.clear()

            con = new sqlconnection(constring)

            dim cmd as sqlcommand
            dim rd as sqldatareader

            if fjobno.length > 0 then
                varsql = "select job.jobno,containerno,cartonfrom,po,style,color,cartonsize,shipment,factory from job,job_line where job.jobno=job_line.jobno and job.jobno like '%" & fjobno & "%' "
            else
                varsql = "select job.jobno,containerno,cartonfrom,po,style,color,cartonsize,shipment,factory from job,job_line where job.jobno=job_line.jobno "
            end if

            varsql = varsql & "order by " & myfield & " " & mysort & ";"

            cmd = new sqlcommand(varsql, con)
            rd = cmd.executereader

            dim lsv as listviewitem

            while rd.read
                lsv = new listviewitem(rd("jobno").tostring)
                'lsv.checked = true

                lsv.subitems.add(rd("containerno").tostring)
                lsv.subitems.add(rd("cartonfrom").tostring)
                lsv.subitems.add(rd("po").tostring)
                lsv.subitems.add(rd("style").tostring)
                lsv.subitems.add(rd("color").tostring)
                lsv.subitems.add(rd("cartonsize").tostring)
                lsv.subitems.add(rd("shipment").tostring)
                lsv.subitems.add(rd("factory").tostring)

                lstvwjobno.items.add(lsv)
            end while
            cursor.current = cursors.default
            return list_dgjobno = 1
        catch ex as exception
            messagebox.show(ex.tostring)
            return list_dgjobno = -1
        finally
            if con.state = connectionstate.open then
                con.close()
            end if
        end try
end function


'获得排序的字段,并显示排序标记
private sub lstvwjobno_columnclick(byval sender as object, byval e as system.windows.forms.columnclickeventargs) handles lstvwjobno.columnclick
        select case e.column.tostring
            case 0
                myfield = "job.jobno"
            case 1
                myfield = "containerno"
            case 2
                myfield = "cartonfrom"
            case 3
                myfield = "po"
            case 4
                myfield = "style"
            case 5
                myfield = "color"
            case 6
                myfield = "cartonsize"
            case 7
                myfield = "shipment"
            case 8
                myfield = "factory"
        end select

        dim i as integer

        for i = 0 to 8
            lstvwjobno.columns(i).text = lstvwjobno.columns(i).text.replace(" ▼", "").replace(" ▲", "")
        next

        if mysort = "asc" then
            lstvwjobno.columns(e.column.tostring).text = lstvwjobno.columns(e.column.tostring).text & " ▼"
            mysort = "desc"
        else
            mysort = "asc"
            lstvwjobno.columns(e.column.tostring).text = lstvwjobno.columns(e.column.tostring).text & " ▲"
        end if

        dim fjobno as string = ""
        fjobno = txtjobno.text.trim()
        r = list_dgjobno(fjobno)
        lstvwjobno.refresh()

end sub


 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表