How to bind a List<string> to a DataGridView control?
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?
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
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.
The following should work as long as you're bound to anything that implements IEnumerable<string>. It will bind the column directly to the string itself, rather than to a Property Path of that string object.
<sdk:DataGridTextColumn Binding="{Binding}" />
链接地址: http://www.djcxy.com/p/60896.html
上一篇: .net 3.5