千家信息网

VB.NET LISTVIEW的相关操作方法和实现的功能有哪些

发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,VB.NET LISTVIEW的相关操作方法和实现的功能有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。在VB.NET开发框架内,同
千家信息网最后更新 2025年01月18日VB.NET LISTVIEW的相关操作方法和实现的功能有哪些

VB.NET LISTVIEW的相关操作方法和实现的功能有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

在VB.NET开发框架内,同样具有很多控件供开发人员使用,方便的实现各种图形界面和程序功能需求。比如我们在这里为大家介绍的VB.NET LISTVIEW就是一款能够帮助我们实现排序的开发控件。

控件:

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

VB.NET LISTVIEW编写源代码:

  1. Imports System.Data.SqlClient

  2. Dim con As New SqlConnection

  3. Dim varSQL As String

  4. Dim r As Integer

  5. Dim conString As String =
    "data source=127.0.0.1;initial
    catalog=mydb; uid=sa;password=123"

'定义默认排序变量

Dim mySort As String = "ASC"

'VB.NET LISTVIEW定义默认字段变量

  1. Dim myField As String
    = "JOB.JOBNO"

'过滤指定条件的记录

  1. Private Sub btnFilter_Click(ByVal
    sender As System.Object, ByVal e
    As System.EventArgs) Handles
    btnFilter.Click

  2. Try

  3. Dim fJobNo As String = ""

  4. fJobNo = txtJobNo.Text.Trim()

  5. r = List_dgJobNo(fJobNo)

  6. Catch ex As Exception

  7. MessageBox.Show(ex.ToString)

  8. Finally

  9. If con.State = ConnectionState.
    Open Then

  10. con.Close()

  11. End If

  12. End Try

  13. End Sub

'VB.NET LISTVIEW显示所有记录

  1. Private Sub btnShowAll_Click
    (ByVal sender As System.Object,
    ByVal e As System.EventArgs)
    Handles btnShowAll.Click

  2. List_dgJobNo("")

  3. End Sub

'函数功能:将读取出来的数据写入lstvwJovNo的表格中
'函数名称:List_dgJobNo,调用格式:List_dgJobNo("10022"),List_dgJobNo("")
'参 数:fJobNo,要查询的工作号JOBNO,为空时则查询所有的
'编 写 者:SD
'日 期:2005-9-23
'转载请保留此信息

  1. Private Function List_dgJobNo(ByVal
    fJobNo As String) As Integer

  2. Try

  3. Cursor.Current = Cursors.WaitCursor

  4. lstvwJobNo.Items.Clear()

  5. con = New SqlConnection(conString)

  6. Dim cmd As SqlCommand

  7. Dim rd As SqlDataReader

  8. If fJobNo.Length > 0 Then

  9. 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 & "%' "

  10. Else

  11. varSQL = "SELECT JOB.JOBNO,CONTAINERNO,
    CARTONFROM,PO,STYLE,COLOR,CARTONSIZE,
    SHIPMENT,FACTORY FROM JOB,JOB_LINE
    WHERE JOB.JOBNO=JOB_LINE.JOBNO "

  12. End If

  13. varSQLvarSQL = varSQL & "ORDER BY "
    & myField & " " & mySort & ";"

  14. cmd = New SqlCommand(varSQL, con)

  15. rd = cmd.ExecuteReader

  16. Dim lsv As ListViewItem

  17. While rd.Read

  18. lsv = New ListViewItem(rd("JOBNO").ToString)

  19. 'lsv.Checked = True

  20. lsv.SubItems.Add(rd("ContainerNo").ToString)

  21. lsv.SubItems.Add(rd("CartonFrom").ToString)

  22. lsv.SubItems.Add(rd("PO").ToString)

  23. lsv.SubItems.Add(rd("Style").ToString)

  24. lsv.SubItems.Add(rd("Color").ToString)

  25. lsv.SubItems.Add(rd("CartonSize").ToString)

  26. lsv.SubItems.Add(rd("ShipMent").ToString)

  27. lsv.SubItems.Add(rd("Factory").ToString)

  28. lstvwJobNo.Items.Add(lsv)

  29. End While

  30. Cursor.Current = Cursors.Default

  31. Return List_dgJobNo = 1

  32. Catch ex As Exception

  33. MessageBox.Show(ex.ToString)

  34. Return List_dgJobNo = -1

  35. Finally

  36. If con.State = ConnectionState.Open Then

  37. con.Close()

  38. End If

  39. End Try

  40. End Function

'VB.NET LISTVIEW获得排序的字段,并显示排序标记

  1. Private Sub lstvwJobNo_ColumnClick
    (ByVal sender As Object, ByVal e As
    System.Windows.Forms.ColumnClickEventArgs)
    Handles lstvwJobNo.ColumnClick

  2. Select Case e.Column.ToString

  3. Case 0

  4. myField = "JOB.JOBNO"

  5. Case 1

  6. myField = "CONTAINERNO"

  7. Case 2

  8. myField = "CARTONFROM"

  9. Case 3

  10. myField = "PO"

  11. Case 4

  12. myField = "STYLE"

  13. Case 5

  14. myField = "COLOR"

  15. Case 6

  16. myField = "CARTONSIZE"

  17. Case 7

  18. myField = "SHIPMENT"

  19. Case 8

  20. myField = "FACTORY"

  21. End Select

  22. Dim i As Integer

  23. For i = 0 To 8

  24. lstvwJobNo.Columns(i).Text =
    lstvwJobNo.Columns(i).Text.Replace
    (" ▼", "").Replace(" ▲", "")

  25. Next

  26. If mySort = "ASC" Then

  27. lstvwJobNo.Columns(e.Column.ToString)
    .Text = lstvwJobNo.Columns(e.Column.
    ToString).Text & " ▼"

  28. mySort = "DESC"

  29. Else

  30. mySort = "ASC"

  31. lstvwJobNo.Columns(e.Column.ToString).
    Text = lstvwJobNo.Columns(e.Column.
    ToString).Text & " ▲"

  32. End If

  33. Dim fJobNo As String = ""

  34. fJobNo = txtJobNo.Text.Trim()

  35. r = List_dgJobNo(fJobNo)

  36. lstvwJobNo.Refresh()

  37. End Sub

看完上述内容,你们掌握VB.NET LISTVIEW的相关操作方法和实现的功能有哪些的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0