如何从代码中的listview中检索列项

时间:2013-08-29 09:12:35

标签: wpf vb.net listview datagrid

我是wpf的新手,正在参加MCTS考试。我已经搜索了2天如何检索代码中的行列项。我已经能够通过创建结构并通过代码添加行项来将数据插入到列表视图中。

Public Structure SimpleData
    Public Property Txt1 As String
        Get
            Return mTxt1
        End Get
        Set(value As String)
            mTxt1 = value
        End Set
    End Property
    Private mTxt1 As String
    Public Property Txt2 As String
        Get
            Return mTxt2
        End Get
        Set(value As String)
            mTxt2 = value
        End Set
    End Property
    Private mTxt2 As String
    Public Property Txt3 As String
        Get
            Return mTxt3
        End Get
        Set(value As String)
            mTxt3 = value
        End Set
    End Property
    Private mTxt3 As String
End Structure
Public Structure MyData
    Public Property Desc() As String
        Get
            Return m_Desc
        End Get
        Set(value As String)
            m_Desc = Value
        End Set
    End Property
    Private m_Desc As String
    Public Property Progress() As Integer
        Get
            Return m_Progress
        End Get
        Set(value As Integer)
            m_Progress = Value
        End Set
    End Property
    Private m_Progress As Integer
    Public Property ProgressText() As String
        Get
            Return m_ProgressText
        End Get
        Set(value As String)
            m_ProgressText = Value
        End Set
    End Property
    Private m_ProgressText As String
    Public Property Pic() As String
        Get
            Return m_Pic
        End Get
        Set(value As String)
            m_Pic = Value
        End Set
    End Property
    Private m_Pic As String
End Structure

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
    Dim sd As New SimpleData
    sd.Txt1 = "Today is"
    sd.Txt2 = "a good day"
    sd.Txt3 = "O YES!"
    listView1.Items.Add(sd)
End Sub

我希望能够检索row(0).Item(0).ToString,这是如何在win表单中检索它。期待回应。提前致谢

2 个答案:

答案 0 :(得分:0)

            Dim constr As String = "Put your connection string here"
            Dim ds As New DataSet
            Dim con As New SqlClient.SqlConnection(constr)
            con.Open()
            Dim sqladap As New SqlClient.SqlDataAdapter("select * from tbl_Employee", con)
            sqladap.Fill(ds)

            For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
                ListView1.Columns.Add(ds.Tables(0).Columns(i).ToString())
            Next
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim listRow As New ListViewItem
                listRow.Text = ds.Tables(0).Rows(i)(0).ToString()
                For j As Integer = 1 To ds.Tables(0).Columns.Count - 1
                    listRow.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
                Next
                ListView1.Items.Add(listRow)
            Next

Read data  from Listview :

Dim name, room, subject, date, period As String
If listviewName.SelectedItems.Count > 0 then
    For i As Integer = 0 To listviewName.SelectedItems.Count - 1
        '*********** transfer selected data on declare String variable ************'
        name= listviewName.SelectedItems(i).SubItems(0).Text
        room = listviewName.SelectedItems(i).SubItems(1).Text
        subject = listviewName.SelectedItems(i).SubItems(2).Text
        date= listviewName.SelectedItems(i).SubItems(3).Text
        period= listviewName.SelectedItems(i).SubItems(4).Text
        '*********** delete **************'
        cmd1.Connection = MYSQLCON
        MYSQLCON.Open()
        cmd1.CommandText = "DELETE FROM tablename WHERE columnname = '" & name & "'"
        reader = cmd1.ExecuteReader
        MYSQLCON.Close()
   Next
End If

答案 1 :(得分:0)

通过将listview项目转换为创建的结构SimpleData然后循环遍历

,我找到了答案
        Dim getitems = CType(listView1.SelectedItem, SimpleData)
        For Each mem In getitems.Txt1
            MsgBox(mem.ToString)
        Next