Getdata function VB.NET

0

I'm trying to create a function to update the Datagridview. I added the following code but a "Can not clean list" error

Code:

    Private Sub GetData()
    Try
        con = New SqlConnection(cs)
        con.Open()
        cmd = New SqlCommand("SELECT nome, matricula, marca, [Licenca-emissao], [licenca-expira] from Clientes order by Nome", con)
        rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        dgw.Rows.Clear()
        While (rdr.Read() = True)
            dgw.Rows.Add(rdr(0), rdr(1))
        End While
        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
    
asked by anonymous 11.11.2015 / 18:48

1 answer

1

Try this:

Private Sub GetData()
    Try
        con = New SqlConnection(cs)
        con.Open()
        cmd = New SqlCommand("SELECT nome, matricula, marca, [Licenca-emissao], [licenca-expira] from Clientes order by Nome", con)
        rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Dim dt = New DataTable()
        dt.Load(rdr)
        dgw.AutoGenerateColumns = True
        dgw.DataSource = dt
        dgw.Refresh()

        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

As I said in the comment, you can not clear the lines because you yourself said you are assigning a DataSource previously, so you need to move it, not the lines in your DataGridView.

    
11.11.2015 / 19:44