Question : Can't set Datagridview data source to list object

Hello I have a subroutine called PopulateBatchLoadDataGridView that I want to use to populate a datagridview.  I've been using the same code in many parts of my application but now in this particular case, when I execute the code, Only the last record is loaded into the datagrid and it is loaded twice.  So for example, it here are three records in the source table, the last records appears three times.  This routine is calling a function called GetTechnologyData that seems to be working fine.  I'm not sure what is going on
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
Private Sub PopulateBatchLoadDataGridView()
        Dim tl As New SortableBindingList(Of TechnologySet)(TechnologySetDB.GetTechnologyData)
        If tl.Count > 0 Then
            dgvTechnologySets.DataSource = tl
        Else
            dgvTechnologySets.DataSource = Nothing
        End If
    End Sub

Public Shared Function GetTechnologyData() As List(Of TechnologySet)
        Dim techlist As New List(Of TechnologySet)
        Dim CnSql As SqlConnection = BadsDB.GetConnection
        Dim cmdA As SqlCommand
        cmdA = New SqlClient.SqlCommand("up_CompareTechnology", CnSql)
        cmdA.CommandType = CommandType.StoredProcedure
        Try
            CnSql.Open()
            Dim reader As SqlDataReader = cmdA.ExecuteReader(CommandBehavior.CloseConnection)
            Dim t As New TechnologySet
            Dim x As Integer = 1
            Do While reader.Read
                t.TechnologySetCode = reader("TechnologySetCode").ToString
                t.TechnologySetName = reader("TechnologySetName").ToString
                t.TechnologySetType = reader("TechnologySetType").ToString
                t.Notes = reader("Notes").ToString
                techlist.Add(t)
            Loop
            Return techlist

        Catch excA As SqlException
            MsgBox(excA.Message)
            Return Nothing
        Finally
            CnSql.Close()
        End Try

Answer : Can't set Datagridview data source to list object

You need to change your code as below.

You need to put Dim t As New TechnologySet in Loop instead of outside.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Public Shared Function GetTechnologyData() As List(Of TechnologySet)
        Dim techlist As New List(Of TechnologySet)
        Dim CnSql As SqlConnection = BadsDB.GetConnection
        Dim cmdA As SqlCommand
        cmdA = New SqlClient.SqlCommand("up_CompareTechnology", CnSql)
        cmdA.CommandType = CommandType.StoredProcedure
        Try
            CnSql.Open()
            Dim reader As SqlDataReader = cmdA.ExecuteReader(CommandBehavior.CloseConnection)
            
            Dim x As Integer = 1
            Do While reader.Read
		Dim t As New TechnologySet
                t.TechnologySetCode = reader("TechnologySetCode").ToString
                t.TechnologySetName = reader("TechnologySetName").ToString
                t.TechnologySetType = reader("TechnologySetType").ToString
                t.Notes = reader("Notes").ToString
                techlist.Add(t)
            Loop
            Return techlist

        Catch excA As SqlException
            MsgBox(excA.Message)
            Return Nothing
        Finally
            CnSql.Close()
        End Try
Random Solutions  
 
programming4us programming4us