Add listview columns with sql server from vb

2

Good, I would like you to help me, I'm doing a Listview that as default I put these values in the columns:

Credit Number

Quota Number

Last Fee

Item

but I want those list view to increase the number of columns as the database has in its fields  My table is called item has this unique field:  . Description  But inside as a record you have these data:  . INSURANCE HOSPITAL ASSISTANCE  . SAFEGUARD INSURANCE

What I want to see in the list view with those columns of the records in the database like this: . Credit Number

Quota Number

Last Fee

Item

INSURANCE HOSPITAL ASSISTANCE

SAFEGUARD INSURANCE

This is my code that I made but if it shows but it shows up as records I want as columns this is my code:

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.ListView1.View = View.Details
    Me.ListView1.GridLines = True
    conn = New SqlConnection("Data Source=********\SQL2012;Initial Catalog=Credito;User ID=****;Password=*****")
    Dim strQ As String = String.Empty
    strQ = "select top 5 Descripcion from credito..Rubro WHERE IdRubro NOT IN (41,42,44,45,1056,1057,1058,48,43) "
    cmd = New SqlCommand(strQ, conn)
    da = New SqlDataAdapter(cmd)
    ds = New DataSet
    da.Fill(ds, "Rubros")
    Dim i As Integer = 0
    Dim j As Integer = 0
    ' adding the columns in ListView
    For i = 0 To ds.Tables(0).Columns.Count - 1
        Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
    Next
    'Now adding the Items in Listview
    For i = 0 To ds.Tables(0).Rows.Count - 1
        For j = 0 To ds.Tables(0).Columns.Count - 1
            itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
        Next
        Dim lvi As New ListViewItem(itemcoll)
        Me.ListView1.Items.Add(lvi)
    Next
End Sub

This is with this code I get the error:

    
asked by PieroDev 17.02.2017 в 18:05
source

1 answer

0

This is the Answer like creating columns with sql server according to the records. Greetings.


 Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.ListView1.View = View.Details
        Me.ListView1.GridLines = True
        ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
        Dim columnHeader1 As New ColumnHeader
        With columnHeader1
            .Text = "Numero de Credito"
            .TextAlign = HorizontalAlignment.Left
            .Width = 146
        End With
        Dim columnHeader2 As New ColumnHeader
        With columnHeader2
            .Text = "Numero Cuota"
            .TextAlign = HorizontalAlignment.Center
            .Width = 142
        End With
        Dim columnHeader3 As New ColumnHeader
        With columnHeader3
            .Text = "Ultima Cuota"
            .TextAlign = HorizontalAlignment.Center
            .Width = 142
        End With

        conn = New SqlConnection("Data Source=192.168.105.150\SQL2012;Initial Catalog=Credito;User ID=sa;Password=DBServ@14")
        Dim strQ As String = String.Empty
        strQ = "select top 2 Descripcion from credito..Rubro WHERE IdRubro NOT IN (41,42,44,45,1056,1057,1058,48,43) "
        cmd = New SqlCommand(strQ, conn)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "Rubros")
        Dim i As Integer = 0
        Dim j As Integer = 0
        ' adding the columns in ListView
        For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                Dim LSet = Me.ListView1.Columns.Add(ds.Tables(0).Rows(i)(j).ToString())
                LSet.Width = 218
            Next

        Next

    End Sub
    
answered by 17.02.2017 / 23:04
source