更新ListViewItem

时间:2011-12-14 11:01:06

标签: asp.net vb.net listview

我在插入另一个项目后,无法找到有关如何更新ListView的正确答案 当我在ListView1.Clear()中添加DatabaseSelect()Button1时,无法查看。

每次插入新项目时如何刷新ListView

以下是填充ListView的代码:

Function DatabaseSelect()
    Try

        myDA.SelectCommand = conn.CreateCommand()
        myDA.SelectCommand.CommandText = ("SELECT * FROM FullName")
        myDA.SelectCommand.CommandType = CommandType.Text
        myDA.Fill(myDS)

        For Each myDR As DataRow In myDS.Tables(0).Rows
            Dim myListview As New ListViewItem()
            myListview.SubItems.Add(myDR(0))
            myListview.SubItems.Add(myDR(1))
            myListview.SubItems.Add(myDR(2))
            myListview.SubItems.Add(myDR(3))
            ListView1.Items.Add(myListview)
        Next
        conn.Open()
        myDA.SelectCommand.ExecuteNonQuery()
        conn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Function

以下是将项目插入ListView的代码:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
        myDA.InsertCommand = conn.CreateCommand()
        myDA.InsertCommand.CommandText = ("INSERT INTO FullName (FirstName,MiddleName,LastName) VALUES(@firstname,@middlename,@lastname)")
        myDA.InsertCommand.CommandType = CommandType.Text
        myDA.InsertCommand.Parameters.Add("@firstname", SqlDbType.NVarChar, 50, "FirstName").Value = TextBox1.Text
        myDA.InsertCommand.Parameters.Add("@middlename", SqlDbType.NVarChar, 50, "MiddleName").Value = TextBox2.Text
        myDA.InsertCommand.Parameters.Add("@lastname", SqlDbType.NVarChar, 50, "LastName").Value = TextBox3.Text
        conn.Open()
        myDA.InsertCommand.ExecuteNonQuery()
        conn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

我认为你必须在Button Click Event中调用Function DatabaseSelect()。

在conn.Close()

之后添加DatabaseSelect()

可能会对你有帮助。