simon
simon

Reputation: 127

Listview in vb.net

Is it possible to display the contents of a two dimensional array vertically on a form in vb.net using listview, and if so how would I do it? So, if my array is declared as dim myarray (2,10) how would I display the contents vertically in listview. All and any help much apprciated, thank you.

Upvotes: 2

Views: 2385

Answers (2)

Ramon Araujo
Ramon Araujo

Reputation: 1738

Every control like ListView in .NET has a marvelous template mechanism what you can use to put HTML inside of it. Moreover you can handle ItemDataBound event and fill a Label located inside the ItemTemplate section of your control with HTML code. Then, use a nested loop to generate your <tr> and <td>, place them on a string and assign it to the Label.Text property.

Hope that helps,

Upvotes: 0

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158369

This method should do the trick for you (I have assumed we are talking about a winforms app, but I realize that it could just as well be an ASP.NET app, in which case my answer might no longer be applicable):

Private Sub ShowArrayInListView(ByVal listView As ListView, ByVal dataArray As String(,))
    listView.Items.Clear()
    For y As Integer = dataArray.GetLowerBound(1) To dataArray.GetUpperBound(1)
        Dim lvi As New ListViewItem

        For x As Integer = dataArray.GetLowerBound(0) To dataArray.GetUpperBound(0)
            If x = 0 Then
                lvi.Text = dataArray(x, y)
            Else
                lvi.SubItems.Add(dataArray(x, y))
            End If
        Next
        listView.Items.Add(lvi)
    Next
End Sub

Upvotes: 0

Related Questions