Search code examples
vb.netdatagridviewdataadapter

Can someone assist me in getting data to display in a DataGridView vb.net?


I didnt have much luck when I posted about this when I was programming this application a different way, so I redid the program using DataAdapter instead of the TableAdapter method.

I need help getting data to display inside the DataGridView tool in vb.net Im not sure what i'm doing wrong here... The data displays in the listbox just fine. but will not display in the DataGridView tool. The DataGridView will display the length of the string. Unsure why... See my code below. (BTW, I created the listbox just for testing purposes and dont want to use that)

What the program does: A user places a checkmark on each house project (ex: cement work) and the project gets the list of tools required for the selected project(s). The project names are in checkboxes and checkChanged event assings the text to the variable "ProjectName" in the SQL select statement.

Private Sub Button1_Click_2(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim connection As OleDbConnection

    Dim cmdString As String = "Select Distinct ToolName,Notes From Tools Where ProjectName = '" & carpentry & "' or ProjectName = '" & cementWork & "' Or ProjectName= '" & dryWall & "' Or ProjectName = '" & electrical _
                & "' Or ProjectName = '" & HVAC & "' Or ProjectName = '" & laminateFlooring & "' Or ProjectName = '" & painting & "' Or ProjectName = '" & plumbing _
                & "' Or ProjectName = '" & texture & "' Or ProjectName = '" & tileWork & "' Or ProjectName = '" & vinylFlooring & "'"

    connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SaintEnterprises.mdb")


    Dim adapter As New OleDbDataAdapter(cmdString, connection)

    Dim tools As New DataSet

    connection.Open()

    adapter.Fill(tools)

    Dim toolslist As New List(Of String)


    ListBox1.Items.Clear()

    For Each row As DataRow In tools.Tables(0).Rows

        Dim s As String = row("toolname").ToString

        'toolslist.Add(row("toolName").ToString) 'Does same thing as line toollist.add(s) below


        toolslist.Add(s.ToString) 'add to List

        ListBox1.Items.Add(s.ToString) 'Displays the type of tools based on the query. 


    Next

    ToolsDataGridView.DataSource = toolslist 'displays the length (Total # of letters) of each toolname'

End Sub

Although I probably wont go back to doing it using Table adapter, if you'd like To see my way of programming this application using TableAdapter, see this link.


Solution

  • I think you need a DataTable to fill your DataGridView. Convert you DataSet-Rows to a DataTable.

    I've tried this and this works fine for me (assuming, your database connection is working).

    Dim oData As New System.Data.DataTable
    
    For Each oRow As System.Data.DataRow In tools.Tables(0).Rows
        oData.ImportRow(oRow)
    Next
    
    ToolsDataGridView.DataSource = oData