VB.NET中LISTVIEW排序

來源:互聯網
上載者:User
排序


控制項:

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


 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。