How to bind a List<string> to a DataGridView control?

agnieszka picture agnieszka · Jan 26, 2009 · Viewed 145.5k times · Source

I have a simple List<string> and I'd like it to be displayed in a DataGridView column.
If the list would contain more complex objects, simply would establish the list as the value of its DataSource property.

But when doing this:

myDataGridView.DataSource = myStringList;

I get a column called Length and the strings' lengths are displayed.

How to display the actual string values from the list in a column?

Answer

Tamanna Jain picture Tamanna Jain · Jul 12, 2010

Try this:

IList<String> list_string= new List<String>();
DataGridView.DataSource = list_string.Select(x => new { Value = x }).ToList();
dgvSelectedNode.Show();

I hope this helps.