views:

2171

answers:

3

it looks pretty easy and there must be a way to do it: i have a simple List and i would like it to be displayed in a column in dataGrid. if these were some more complex objects i would know how to set the displayed texts in the columns but i want to display just strings and if i set the list as a datagrid's data source myDataGrid.DataSource = myStringList; i get a column called Length and the strings' lengths are displayed. how can i do it?

+3  A: 

Thats because DataGridView looks for properties of containing objects. For string there is just one property - length. So, you need a wrapper for a string like this

public class StringValue
{
    public StringValue(string s)
    {
        Value = s;
    }
    public string Value { get { return _value; } set { _value = value; } }
    string _value;
}

Then bind List<StringValue> object to your grid. It works

sinm
+4  A: 

you can also use linq and anonymous types to achieve the same result with much less code as described here.

jarek
+2  A: 

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.

Tamanna Jain