Need Help With A DataGridView Problem

Jim Strutz 20 Reputation points
2023-03-23T13:36:27.2766667+00:00
I need help with a DataGridView application, can't find an answer to this particular problem online. In my application, a user selects an order number from a combo box and the software reaches out to a database to retrieve the record and it's associated order items. The data is displayed to the form's text boxes, etc. and the order items are supposed to be displayed in a DataGridView control - but this isn't working. The code for this is below.

Structure cGridItem
    Dim Qty As Integer
    Dim Desc As String
    Dim Cost As Decimal
    Dim Price As Decimal
    Dim Shipdate As Date
End Structure

Dim GridList As New BindingList(Of cGridItem)

GridList.Add(New cGridItem With {.Qty = 1, .Desc = "hello", .Cost = 3.14, .Price = 4.56, .Shipdate = Now})
GridList.Add(New cGridItem With {.Qty = 3, .Desc = "peace", .Cost = 6.78, .Price = 8.9, .Shipdate = Now})
GridList.Add(New cGridItem With {.Qty = 5, .Desc = "please", .Cost = 3, .Price = 4, .Shipdate = Now})

ItemGrid.DataSource = GridList


Normally, this would iterate through all the order item records to create a GridList but here I hardcoded three fixed "records" to display. The DataGridView, named "ItemGrid", shows three new rows but with no data in the cells. I can change the number of GridList items and the ItemGrid will display the correct number of rows but with empty cells. This is driving me crazy, thanks for any help.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,713 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2023-03-23T13:51:07.47+00:00

    Hi

    Slight changes to your code. The example below works fine.

    ' Form1 with blnk DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim GridList As New BindingSource
    	Class cGridItem
    		Property Qty As Integer
    		Property Desc As String
    		Property Cost As Decimal
    		Property Price As Decimal
    		Property Shipdate As Date
    	End Class
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		GridList.Add(New cGridItem With {.Qty = 1, .Desc = "hello", .Cost = 3.14D, .Price = 4.56D, .Shipdate = Now})
    		GridList.Add(New cGridItem With {.Qty = 3, .Desc = "peace", .Cost = 6.78D, .Price = 8.9D, .Shipdate = Now})
    		GridList.Add(New cGridItem With {.Qty = 5, .Desc = "please", .Cost = 3, .Price = 4, .Shipdate = Now})
    
    		DataGridView1.DataSource = GridList
    
    	End Sub
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.