We help IT Professionals succeed at work.

[LINQ] Problem binding data to drop down box

The DataValueField contains the appropriate data, but the DataTextField shows up as

System.Linq.Enumerable+WhereSelectEnumerableIterator'2[System.String, System.String]

Full code for the sub is below. Basically i need the full path to each file but in the drop down box i only want to display the file name, hence the linq filter.

        Dim Table1 As DataTable
        Table1 = New DataTable("DAT List")

        Dim datFiles As DataColumn = New DataColumn("Files")
        datFiles.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(datFiles)

        Dim datName As DataColumn = New DataColumn("Name")
        datFiles.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(datName)

        Dim datPath As String = txtPath.Text & "\SnapDriveInfo\"
        Dim datFile As String() = IO.Directory.GetFiles(txtPath.Text & "\SnapDriveInfo\")
        Dim file = From p In datFile Where p.Contains(".dat") Select p
        'Dim logName = From q In file Where q.Contains(".dat") Select q.Replace(datPath, String.Empty).Trim
        Dim logName = From q In file Where q.Contains(".dat") Select q.Replace(datPath, String.Empty).Trim


        For Each log In file
            txtOut.Text &= log
            txtOut.Text &= vbCrLf

            Dim Row1 As DataRow
            Try
                Row1 = Table1.NewRow()
                Row1.Item(datFiles) = log
                Row1.Item(datName) = logName
                Table1.Rows.Add(Row1)
            Catch ex As Exception

            End Try


        Next

        drpDatFiles.DataSource = Table1
        drpDatFiles.DataTextField = Table1.Columns(1).ToString
        drpDatFiles.DataValueField = Table1.Columns(0).ToString
        drpDatFiles.DataBind()

Open in new window

Comment
Watch Question

BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Change

        drpDatFiles.DataTextField = Table1.Columns(1).ToString
        drpDatFiles.DataValueField = Table1.Columns(0).ToString

to

        drpDatFiles.DataTextField = "Name"
        drpDatFiles.DataValueField = "Files"

Author

Commented:
I made that change and the behavior is still the same.
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Move

drpDatFiles.DataSource = Table1

to after the above two lines

Author

Commented:
Still doesn't change the drop down box output.
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try changing

 Try
                Row1 = Table1.NewRow()
                Row1.Item(datFiles) = log
                Row1.Item(datName) = logName
                Table1.Rows.Add(Row1)
            Catch ex As Exception

            End Try


to


 Try
                Row1 = Table1.NewRow()
                Row1.Item(datFiles) = log
                Row1.Item(datName) = IO.Path.GetFileName(log)
                Table1.Rows.Add(Row1)
            Catch ex As Exception

            End Try

Author

Commented:
here is something interesting. if i use table1.columns(0).tostring for the text field i get the full path to the file displayed properly. but table1.columns(1).tostring doesnt and column 1 contains the text i want displayed.

Author

Commented:
That did the trick. Thank you!
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad to help :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.