Getting BindingSource Row by Column

NachitoMax 416 Reputation points
2021-08-18T23:48:44.33+00:00

Hey

I have a BindingSource on my form that collects data from a table. I am showing 3 columns out of 17 in a grid and need to capture the remaining 14 column values for a row. I can assign the BindingSource to a text box to display the specific value but how can i do it in code to save the values into a collection of properties?

I have tried this currently which is throwing an error (Cannot cast to type)

ThermalBreak = directCast(ProjectDataBindingSource.Current, DataViewRow).Item("_thermal_break").ToString

i have also tried

ThermalBreak = directCast(ProjectDataBindingSource.Current, DataViewRow).Item(0).ToString

Thanks

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. NachitoMax 416 Reputation points
    2021-08-19T03:19:42.27+00:00

    Hey

    I figured it out. The DataSource to the ProjectDataBindingSource is a class, not a DataTable

    Imports System.ComponentModel
    
    Public Class ProjectData
    
        Public Property _job_id As String
        Public Property _name As String
        Public Property _thermal_break As String
    
        Public Sub New(ByVal job_id As Integer, ByVal name As String, ByVal thermalbreak As String)
    
            Dim ConvJobID As String = Convert.ToString(job_id.ToString("00000"))
    
            _job_id = ConvJobID
            _name = name
            _thermal_break = thermalbreak
    
        End Sub
    
        Public Shared Function GetProjectData() As BindingList(Of ProjectData)
            Dim JL As JOB.BLL.JobList = New JOB.BLL.JobList
            Dim GetList As List(Of JOB.BLL.JobList_details) = JL.GetJobListByAll()
    
            Dim List As BindingList(Of ProjectData) = New BindingList(Of ProjectData)
            For Each row As JOB.BLL.JobList_details In GetList
    
                List.Add(New ProjectData(row._job_id, row._name, row._thermal_break))
            Next
            Return List
    
        End Function
    
    End Class
    

    Then in my form, i bind a Grid & some TextBoxes to the relavent BindingSourse fields.
    Finally , to return the hidden column values in code, i use this

    Dim t As ProjectData = CType(projectDataBindingSource.Current, ProjectData)
    

    Which provides access to the ProjectData members

    Dim _ID As String = t. _job_id
    Dim _Name As String = t._name
    Dim _TB As String = t._thermal_break
    
    0 comments No comments

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.