DGV not showing data from SQL query

  • Thread starter Thread starter Pete -
  • Start date Start date
P

Pete -

Guest
I have a DGV that loads just fine on another DGV in my app.

My question is why doesn't the same code work on a 2nd DGV.

DataGridView2 is all setup on form.load:

' DGV inventory view setup
ViewInventory.DataGridView2.Columns.Add(0, "Name")
ViewInventory.DataGridView2.Columns.Add(1, "Quantity on Hand")
ViewInventory.DataGridView2.Columns.Add(2, "Price")
ViewInventory.DataGridView2.Columns(0).Width = 200
ViewInventory.DataGridView2.Columns(1).Width = 100
ViewInventory.DataGridView2.Columns(2).Width = 50

' do not show row header
ViewInventory.DataGridView2.RowHeadersVisible = False

' do not allow click in the header area
For m As Integer = 0 To ViewInventory.DataGridView2.ColumnCount - 1
ViewInventory.DataGridView2.Columns(m).SortMode = DataGridViewColumnSortMode.NotSortable
Next

ViewInventory.DataGridView2.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ViewInventory.DataGridView2.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
ViewInventory.DataGridView2.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ViewInventory.DataGridView2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
ViewInventory.DataGridView2.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ViewInventory.DataGridView2.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

ViewInventory.DataGridView2.ReadOnly = True
' sort dgv ascending
ViewInventory.DataGridView2.Sort(ViewInventory.DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

I have a button control to click and start the 'ViewInventory' form:

Private Sub ViewInventory_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.Show()

' get data and put into dgv *****************************************************
DataGridView2.Rows.Clear()
Functions.LoadDgvInventoryView()

DataGridView2.Rows(0).Selected = True

PictureBox1.Refresh()
PictureBox1.Image = Nothing

DataGridView2.Focus()

MsgBox(DataGridView2.CurrentRow.Cells(0).Value.ToString)

'get selected mold name data from name column and assign to local var
inventoryImage = DataGridView2.CurrentRow.Cells(0).Value.ToString

PictureBox1.Refresh()

Dim imgFileJpg As String = StartForm.GlobalVariables.ImgFolder & inventoryImage & ".jpg"
Dim imgFilePng As String = StartForm.GlobalVariables.ImgFolder & inventoryImage & ".png"

If System.IO.File.Exists(imgFileJpg) Then
PictureBox1.Image = Image.FromFile(imgFileJpg)
ElseIf System.IO.File.Exists(imgFilePng) Then
PictureBox1.Image = Image.FromFile(imgFilePng)
Else
PictureBox1.Image = Image.FromFile("C:\StatuarySalesPlus\images\nopic.png")
End If

End Sub

And lastly, my function that is called from the 'ViewInventory' form:

Public Function LoadDgvInventoryView()

Try

Using con As OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\StatuarySalesPlus\DB\SSPOS.accdb")
con.Open()
Dim ds As DataSet = New DataSet
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String

sql = "SELECT ProductName, QtyOnHand, ProductPrice FROM Inventory"

adapter.SelectCommand = New OleDb.OleDbCommand(sql, con)

adapter.Fill(ds)

'For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
' Dim someVar As String = (ds.Tables(0).Rows(i)(0).ToString())
' MsgBox(someVar)
'Next

ViewInventory.DataGridView2.DataSource = ds.Tables(0)

con.Close()
con.Dispose()

End Using

Catch
End Try

Return Nothing
End Function

As you see I have inserted a msgbox (temporarily commented out) to test the data contents of the dataset table. It returns the correct values from the DB. But never gets loaded into the DGV as it returns this error on executing the form load for 'InventoryView':

1406551.png



Here is the data being extracted from the DB for the DGV:

1406557.png

I would appreciate a little help on this one- could be simple that I am not seeing.

The msgbox that is in use before the actual variable assignment is to see the value before executing.

Thanks in advance, Pete

Continue reading...
 
Back
Top