none
LINQ issue with datagridview RRS feed

  • Question

  • I have been scratching my head on this and not sure what is going on.  I just started a new Win Forms application, added a dbml file to database and tables, and all I want to do is pull name data from a Provider table.  So I have this and the result in the dgv is a LENGTH, which I don't want.  And what makes this more head shaking is I just finished a different project where I am using the same syntax and it is working fine:

     Dim pList = (From p In PDDB.Providers
                             Select p.LastName.ToUpper).ToList
                DataGridView1.DataSource = pList
    UPDATE TO THIS:  I created a class for ProviderName with properties for Last Name, First Name, and Title and it works, however now I want to concatenate those to create a 'FullName'


    Brad Allison


    Tuesday, June 25, 2019 4:34 PM

Answers

  • Something like this

    Public Class Provider
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 25, 2019 6:54 PM
    Moderator
  • Excellent!  Thanks for this.  My next step is trying to now bind the object to a listbox, but then assign the ID of the provider as ValueMember

    Brad Allison

    Set the DataSource of the ListBox to a List(Of Provider) and override ToString with the property you want to show in the ListBox.

    Public Class Provider
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return "TODO"
        End Function
    End Class
    

    To get the value member do this instead CType(SomeListBox.SelectedItem,Provider).ID for example

    Like this perhaps

    Public Class Provider
        Public Property ID() As Integer
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return FullName
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 25, 2019 7:05 PM
    Moderator

All replies

  • Something like this

    Public Class Provider
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 25, 2019 6:54 PM
    Moderator
  • Excellent!  Thanks for this.  My next step is trying to now bind the object to a listbox, but then assign the ID of the provider as ValueMember

    Brad Allison

    Tuesday, June 25, 2019 6:58 PM
  • Excellent!  Thanks for this.  My next step is trying to now bind the object to a listbox, but then assign the ID of the provider as ValueMember

    Brad Allison

    Set the DataSource of the ListBox to a List(Of Provider) and override ToString with the property you want to show in the ListBox.

    Public Class Provider
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return "TODO"
        End Function
    End Class
    

    To get the value member do this instead CType(SomeListBox.SelectedItem,Provider).ID for example

    Like this perhaps

    Public Class Provider
        Public Property ID() As Integer
        Public Property Title() As String
        Public Property FirstName As String
        Public Property LastName() As String
        Public ReadOnly Property FullName() As String
            Get
                Return $"{Title} {FirstName} {LastName}"
            End Get
        End Property
    
        Public Overrides Function ToString() As String
            Return FullName
        End Function
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 25, 2019 7:05 PM
    Moderator