Click here to Skip to main content
15,499,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I might call this more of a rant than a question

To Skip the RANT go to OK lets get to the questions
VS 2019 the DataGridView (DGV) is like that old saying "You Can Have Your Cake and Eat To"

EXCEPT with the DGV you only get the cake NO eating it

After a lot of searching and trial and error playing with DGV it is so wonderful to just write
dgvOne.DataSource = dt
and you have data displayed


My issue is when you use this technique NO manually adding to the DGV>br/>
Why the OCD rant unless you fill it with data the DGV has this ugly gray space below my once a month entry really NOT a nice looing user EX


OK lets get to the questions

I know how to manually add the column headers with code and properties

I am assuming I can SELECT FROM the TABLE put these values in a StringArray

I seldom embrace Arrays I always thought of them as DB that disappear


So how do I loop through the String Array and add the values as Rows to the DGB

AND have the DGV pre populated with 12 empty rows?

Code Below is what I have tried


What I have tried:

VB
'Private Sub aA(getListOfMotonum() As List(Of String))
Public Sub aA()
    'Code Below makes empty ROWS in DGV
    ''dgvOne.Columns.AddRange(Enumerable.Range(0, 2).Select(Function(x) New DataGridViewTextBoxColumn).ToArray)
    ''dgvOne.Rows.AddRange(Enumerable.Range(0, 10).Select(Function(x) New DataGridViewRow).ToArray)
    Dim SQL As String = "Select * FROM ParentTable"
    'Dim SQL As String = "SELECT COUNT(*) FROM ParentTable"
    Dim output As New List(Of String)()
    Dim dbName As String = "Word.db"
    'Dim cmd As New SQLiteCommand


    ' Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
    Using conn = New SQLiteConnection("Data Source =" & dbName & ";Version=3;")
        Using cmd = New SQLiteCommand(SQL, conn)
            conn.Open()

            Try
                Dim dr = cmd.ExecuteReader()
                While dr.Read()

                    output.Add(dr("NAME").ToString())
                    'Dim output As New List(Of String)()
                    rtbEnter.Text = dr("NAME").ToString()
                    'rtbEnter.Text = output.ToArray()

                End While
            Catch e As SqlClient.SqlException
                ' Do some logging or something.
                MessageBox.Show("There was an error accessing your data. DETAIL: " & e.ToString())
            End Try
        End Using
    End Using
    Dim str1 As String, str2 As String
    Dim L As Integer
    For Each item As String In output
        lbOne.Items.Add(item)
        L = Len(item)
        str1 = CStr(L)
        str2 = output(0)
    Next
    'dgvOne.ColumnCount = 3
    'dgvOne.Columns(0).Name = "PID"
    'dgvOne.Columns(1).Name = "Entry Data"
    Dim dt As New DataTable
    dt.Columns.Add("ID")
    dt.Columns.Add("Name")
    dt.Columns(0).AutoIncrement = True
    Dim R As DataRow = dt.NewRow
    R("Name") = str1 & " " & str2
    dt.Rows.Add(R)
    dgvOne.DataSource = dt

    'Return output
End Sub
Posted
Updated 14-Jul-20 6:49am
v2
Comments
Richard MacCutchan 14-Jul-20 4:07am    
I am not sure I understand what you are complaining about, but filling DataGridView controls manually or via a binding source works fine. But trying to do both makes no sense since one will overwite the other.

In your example code you appear to be trying to do both methods. Pick one. See the comment from @Richard-MacCutchan.

If you go down the datatable route
VB
dgvOne.DataSource = dt
then add 12 blank rows to the datatable before assigning it to the DataSource.

If you go down the route of manually adding the rows to the datagridview then just add 12 more blank rows in a separate loop after adding your data.

One or the other. Not both.

Other comments
- if you are going to the trouble of stepping through each record in your result set then do all the work within that loop. Not sure why you have a another for-each loop.

- In that for-each loop
For Each item As String In output
you are continuously overwriting L, str1 and str2. I suspect that you wanted the
VB
R("Name") = str1 & " " & str2
dt.Rows.Add(R)
actually inside that loop

- No need for arrays anywhere in this. Nor have you actually used one.

Finally - a bit of advice for getting quicker and better answers ... get rid of commented out lines in the code you present to us here. Especially if you are not using the correct lang="VB" in your post. It just clutters up your question, making it more difficult for people to read. Many members will just move on to the next question, which doesn't help you very much.
 
Share this answer
 
Comments
Choroid 14-Jul-20 11:48am    
@Richard-MacCutchan Thanks I agree less commented out code would be ideal Guess I have become complaint shy from StackOverflow use where You did not show us enough of what you tried. I will post my completed code below for others that might want to see the finished product My frustration with this issue is I did this in JavaFX in about 10 min as a novas
Here is the completed code from the Solution Above


Private Sub PopulateDGV()

    Dim str2 As String
    Dim s1 As Integer
    Dim dbName As String = "Word.db"
    Dim conn As New SQLiteConnection("Data Source =" & dbName & ";Version=3;")
    Dim valuesList As ArrayList = New ArrayList()
    conn.Open()

    'Read from the database
    Dim cmd As SQLiteCommand = New SQLiteCommand("Select * FROM ParentTable", conn)
    Dim rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader

    'Set Design of the DataGridView
    dgvOne.DefaultCellStyle.Font = New Font("Tahoma", 10)
    dgvOne.ColumnCount = 2
    dgvOne.Columns(0).Width = 60
    dgvOne.Columns(1).Width = 420

    'DGV Header Names
    dgvOne.Columns(0).Name = "PID"
    dgvOne.Columns(1).Name = "Entry Data"

    'Read from DB Table add to DGV row
    While rdr.Read()
        valuesList.Add(rdr(1)).ToString()
        lbOne.Items.Add(rdr(1)).ToString()
        s1 = rdr(0).ToString
        str2 = rdr(1)
        dgvOne.Rows.Add(s1, str2)
    End While

    'Add Blank rows to DGV
    For iA = 1 To 4
        dgvOne.Rows.Add(" ")
    Next

    conn.Close()

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900