Question Help with DataGridView

dgedinyte

New member
Joined
Apr 20, 2015
Messages
3
Programming Experience
3-5
Hey All,

I'm writing a program in which I need to populate a DataGridView table. I have never before used one of these but I get the impression that it works kind of like a pivot table in excel. I would like to populate the table with data from a delimited text file. My initial research suggested that you could add information from the text file to a BindingList, and then use the BL as the datasource for the DGV. Well I tried that and it goes through the motions but it leaves the DGV table cells blank. Here's the code. I am clearly doing something wrong. Here's hoping you know what it is. Oh I suppose it's worth mentioning that I tried a regular dim list = new list(of array), first and got nothing then I moved to a BindingList of string.
Public Shared Function OpenText4(Optional ByVal path As String = "SOMEPATH.txt") As System.IO.StreamReader
        OpenText4 = System.IO.File.OpenText(path)
        Dim fullfilestring As String
        Dim c1() As String
        Dim Blist = New BindingList(Of String)
        Dim ListBindingSource As New BindingSource
        Do Until OpenText4.Peek = -1
            For Each row In path
                fullfilestring = OpenText4.ReadLine
                If fullfilestring = Nothing Then
                Else
                    c1 = fullfilestring.Split(",")
                    Blist.Add(c1)
                End If
            Next
        Loop
        OpenText4.Close()
        ListBindingSource.DataSource = Blist
        Try
            With FrmInsInf.DGV1
                .AutoGenerateColumns = False
                .DataSource = ListBindingSource
            End With
        Catch ex As Exception
        End Try
    End Function
End Class
 
Last edited by a moderator:
Let's concentrate on these four lines:
Dim c1() As String
Dim Blist = New BindingList(Of String)
'...
c1 = fullfilestring.Split(",")
Blist.Add(c1)
Do you see anything wrong there? You declare `c1` as a String array, then you try to add it to a collection of Strings. Can you put an egg carton where an egg is expected? If not then why would you expect to be able to put a String array where a String is expected?

You need to give the grid something that it can actually display. That means a collection of objects that have distinct properties or columns. Firstly, I would suggest that you read the file using a TextFieldParser. That will avoid some problems that can arise using String.Split. The documentation for the TextFieldParser class includes a code example.

I would suggest two possible options for storing the data in a form useful to your aim of displaying it in a DataGridView:

1. Create a DataTable with a column for each column you want in the grid. As you read the data from the file, add the fields from the current line to a row in the DataTable. When you're done, bind the DataTable to the BindingSource and that to the grid.

2. Define your own class that has a property for each column you want in the grid. As you read the data from the file, create an instance of that type and populate the properties with the fields from the current line and add the object to a List of that type. When you're done, bind the List to the BindingSource and that to the grid.

By the way, are you adding the columns to the grid in the designer? If not then you're going to want to NOT set AutoGenerateColumns to False.
 
jmcilhiney,

Thanks for the reply. I am going to give it a try. I did have columns already added to he grid. I think I'll try it without predefined columns though.
 
Thanks again jmcilhiney! I just didn't realize how involved it was. Here's the code that finally worked:

Public Class FrmInsInf


    Dim connectionString As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="c:\SomePath\Master.accdb")

    Public Sub FrmInsInf_LoadForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load

        With Dgv
            .DataSource = Nothing
            .Rows.Clear()
            .ColumnCount = 10
            .Columns(0).Width = 80
            .Columns(0).HeaderText = "Inst ID"
            .Columns(1).Width = 100
            .Columns(1).HeaderText = "Inst Descr"
            .Columns(2).Width = 35
            .Columns(2).HeaderText = "Volts"
            .Columns(3).Width = 35
            .Columns(3).HeaderText = "Amps"
            .Columns(4).Width = 100
            .Columns(4).HeaderText = "Model#"
            .Columns(5).Width = 100
            .Columns(5).HeaderText = "Serial#"
            .Columns(6).Width = 35
            .Columns(6).HeaderText = "Lab"
            .Columns(7).Width = 110
            .Columns(7).HeaderText = "SAP Tag"
            .Columns(8).Width = 110
            .Columns(8).HeaderText = "Asset Tag"
            .Columns(9).Width = 100
            .Columns(9).HeaderText = "Test"
        End With
        LoadData()



    End Sub

    Public Sub LoadData()
        Dim ss As String
        ss = "select * FROM Instruments"
        Dim LoadAdaptor As New OleDbDataAdapter(ss, connectionString)
        Dim LoadDataSet As New DataSet
        Dim RowsCount As Integer

        If connectionString.State = ConnectionState.Closed Then
            connectionString.Open()
            LoadAdaptor.Fill(LoadDataSet, "Instruments")
            RowsCount = LoadDataSet.Tables(0).Rows.Count
            If RowsCount < 1 Then
                MsgBox("There are no records")
                LoadDataSet.Reset()
                connectionString.Close()
            Else
                ' there is data
                Dgv.Rows.Add(RowsCount)
                For i As Integer = 0 To RowsCount - 1
                    With Dgv
                        .Rows(i).Cells(0).Value = LoadDataSet.Tables(0).Rows(i).Item(0)
                        .Rows(i).Cells(1).Value = LoadDataSet.Tables(0).Rows(i).Item(1)
                        .Rows(i).Cells(2).Value = LoadDataSet.Tables(0).Rows(i).Item(2)
                        .Rows(i).Cells(3).Value = LoadDataSet.Tables(0).Rows(i).Item(3)
                        .Rows(i).Cells(4).Value = LoadDataSet.Tables(0).Rows(i).Item(4)
                        .Rows(i).Cells(5).Value = LoadDataSet.Tables(0).Rows(i).Item(5)
                        .Rows(i).Cells(6).Value = LoadDataSet.Tables(0).Rows(i).Item(6)
                        .Rows(i).Cells(7).Value = LoadDataSet.Tables(0).Rows(i).Item(7)
                        .Rows(i).Cells(8).Value = LoadDataSet.Tables(0).Rows(i).Item(8)
                        .Rows(i).Cells(9).Value = LoadDataSet.Tables(0).Rows(i).Item(9)
                    End With
                Next
            End If
            LoadDataSet.Reset()
            connectionString.Close()
        Else
            'connection already open
            LoadAdaptor.Fill(LoadDataSet, "Instruments")
            RowsCount = LoadDataSet.Tables("Instruments").Rows.Count
            If RowsCount < 1 Then
                MsgBox("There are no records")
                LoadDataSet.Reset()
                connectionString.Close()
            Else
                ' there is data
                Dgv.Rows.Add(RowsCount)
                For i As Integer = 0 To RowsCount - 1
                    With Dgv
                        .Rows(i).Cells(0).Value = LoadDataSet.Tables(0).Rows(i).Item(0)
                        .Rows(i).Cells(1).Value = LoadDataSet.Tables(0).Rows(i).Item(1)
                        .Rows(i).Cells(2).Value = LoadDataSet.Tables(0).Rows(i).Item(2)
                        .Rows(i).Cells(3).Value = LoadDataSet.Tables(0).Rows(i).Item(3)
                        .Rows(i).Cells(4).Value = LoadDataSet.Tables(0).Rows(i).Item(4)
                        .Rows(i).Cells(5).Value = LoadDataSet.Tables(0).Rows(i).Item(5)
                        .Rows(i).Cells(6).Value = LoadDataSet.Tables(0).Rows(i).Item(6)
                        .Rows(i).Cells(7).Value = LoadDataSet.Tables(0).Rows(i).Item(7)
                        .Rows(i).Cells(8).Value = LoadDataSet.Tables(0).Rows(i).Item(8)
                        .Rows(i).Cells(9).Value = LoadDataSet.Tables(0).Rows(i).Item(9)
                    End With
                Next
            End If
            LoadDataSet.Reset()
            connectionString.Close()

        End If
    End Sub

   


End Class
 
Last edited by a moderator:
Back
Top