If I bind a DataGridView.DataSource
to List(Of String)
, the length of the strings are displayed, rather than the strings themselves.
How do I display the string values instead?
(This is a VB.NET translation of this answer: https://stackoverflow.com/a/3225897/741136)
When binding a list of items to a DataGridView, the grid will create a column for each public property of the items. The items are usually data classes with properties of types like String, Integer, etc. If your items are Strings then the grid will do the same thing, creating columns for the public properties of the String class. The String class only has the property Length
, which is what you are seeing.
To solve this, we can use LINQ to wrap the strings in an anonymous type with a .Value
property:
Dim ignoredFilenames As New List(Of String)
ignoredFilenames.Add("thumbs.db")
ignoredFilenames.Add("desktop.ini")
DataGridView1.DataSource = ignoredFilenames.Select(Function(x) New With {.Value = x}).ToList()