Visual basic -Listview data by tables

Asked

Viewed 79 times

0

    Dim sqlConString As String = "Server=localhost\TESTE;Database=tempTest;User Id=sa;Password=123"
            Try
                Using con = New SqlConnection(sqlConString)
                    con.Open()
                    Using cmd = con.CreateCommand
                '' Para filtrar 
                cmd.CommandText = "SELECT * FROM Clientes where NIF LIKE  '" + TextBox1.Text + "%'"
                cmd.CommandText = "select * from Clientes"
                Using reader = cmd.ExecuteReader
                    Dim counter As Int32 = 0
                    While reader.Read
                        ListView1.Items.Add(reader.GetString(reader.GetOrdinal("NIF")), counter)
                        counter = counter + 1
                        ListView1.Items.Add(reader.GetString(reader.GetOrdinal("Nome")), counter)
                        counter = counter + 1
                        ListView1.Items.Add(reader.GetString(reader.GetOrdinal("Telefone")), counter)
                        counter = counter + 1
                        ''MsgBox(reader.GetString(reader.GetOrdinal("NIF")))
                    End While
                End Using
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try

I want you to be by every table like this https://imgur.com/a/qGAdl

  • So you will hardly get help from anyone, edit and put the code correctly and explain your question better!

  • Your question is confused... this code is not VB6 (.NET maybe?)

  • This is VB my doubt is in each of the Listview1.item.Add I want to put by table for example the "NIF" I want it to be in the first column of the table under the NIF and so succinctly

  • That’s how I found out

  • Dim item As New ListViewItem(reader.GetString(reader.GetOrdinal("NIF")))
 item.SubItems.Add(reader.GetString(reader.GetOrdinal("Nome")))
 item.SubItems.Add(reader.GetString(reader.GetOrdinal("Contacto"))) Listview1.Items.Add(item)

  • Why are you carrying the property CommandText twice? Check out this article here: https://mobile.codeguru.com/vb/controls/vbnet_controls/listview/article.php/c3979/How-to-Fill-a-ListView-with-any-ADONet-Dataset.htm.

  • You really need to use the ListView? If you used one DataGridView instead, you could upload a Dataset instead of a Datareader and play it straight to the property DataGridView.DataSource, that he would already create the columns and rows. Of course you would have to change your query to return only the 3 columns you want, and in the order you want, such as: SELECT NIF, Nome, Telefone FROM Clientes.

Show 2 more comments

1 answer

0

Good afternoon, follows example of how you could do using the one Datagridview.

Dim sql = "SELECT IdTipoAlimento, NomeTipoAlimento, NomeAlimento, QtdProteina FROM muscle.tb_alimentos"
Dim dt As Object = DAL.AcessoBD.ExecutarComando(sql, CommandType.Text, Nothing, DAL.AcessoBD.TipoDeComando.ExecuteDataSet)

For intCount As Integer = dt.Tables(0).Rows.Count - 1 To 0 Step -1
     Me.grdAlimento.Rows.Add(dt.Tables(0).Rows(intCount)("IdTipoAlimento"), dt.Tables(0).Rows(intCount)("NomeAlimento"), dt.Tables(0).Rows(intCount)("NomeTipoAlimento"), dt.Tables(0).Rows(intCount)("QtdProteina"))
     intCount2 = intCount2 + 1
Next

Browser other questions tagged

You are not signed in. Login or sign up in order to post.