我有一个IList<string>
从函数返回(如可变LST)和I组,然后我.NET/C#绑定的IList <string>到一个DataGridView
this.dataGridView1.DataSource = lst;
数据网格添加一列标记的长度,然后列出了每个字符串的长度。 如何让它只列出字符串?
我有一个IList<string>
从函数返回(如可变LST)和I组,然后我.NET/C#绑定的IList <string>到一个DataGridView
this.dataGridView1.DataSource = lst;
数据网格添加一列标记的长度,然后列出了每个字符串的长度。 如何让它只列出字符串?
您确实需要一个拥有字符串属性的对象列表。在.NET 3.5,你可以欺骗:
.DataSource = list.Select(x=>new {Value = x}).ToList();
否则创建一个虚拟类,并在手动将数据复制...
这是因为DataGridViews显示对象的属性。在这种情况下,List只有一个属性“Length”,因此它只能显示“Lenght”(与DataType无关)。 你需要创建一个包装类来实现你想要的(一个“MyString”类的属性为“Text”,然后在你的网格中显示一个List)。
希望这有助于
添加代码示例
class MyString
{
private string _text;
public string Text
{ get
{
return _text;
}
set
{
_text = value;
}
}
}
“在执行形式
private List<MyString> foo()
{
List<MyString> lst = new List<MyString>();
MyString one = new MyString();
MyString two = new MyString();
one.Text = "Hello";
two.Text = "Goodbye";
lst.Add(one);
lst.Add(two);
return lst;
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = foo();
}
请问您的IList只包含字符串?还是它包含一些其他类型,'`? –
Nate
2009-07-09 14:50:18
呃......他说这是一个IList ...... –
Clyde
2009-07-09 14:57:23