listBox是可以的:
StringCollection a = new StringCollection();
a.Add("abcd");
a.Add("东南西北");
a.Add("恭喜发财");
a.Add("你说什么");
a.Add("abcdzzzzzzzzz");
listBox1.DataSource = a;但是换成DataGrid,只绑定显示了字符串的 lengh 属性,和 listBox 最关键的区别在哪里呢?
StringCollection a = new StringCollection();
a.Add("abcd");
a.Add("东南西北");
a.Add("恭喜发财");
a.Add("你说什么");
a.Add("abcdzzzzzzzzz");
listBox1.DataSource = a;但是换成DataGrid,只绑定显示了字符串的 lengh 属性,和 listBox 最关键的区别在哪里呢?
private void Page_Load(object sender, System.EventArgs e)
{
System.Collections.Specialized.StringCollection a = new System.Collections.Specialized.StringCollection();
a.Add("abcd");
a.Add("东南西北");
a.Add("恭喜发财");
a.Add("你说什么");
a.Add("abcdzzzzzzzzz");
this.DataGrid1.DataSource =a;
this.DataGrid1.DataBind();}
代码没有出错,但是效果不对!我写了:“只绑定显示了字符串的 lengh 属性”
class MyString
{
private string _value;
public string Value
{
get{ return _value;}
set{ _value = value;}
} public MyString( ):this("")
{
}
public MyString( string sValue )
{
_value = sValue;
}
public static implicit operator MyString( string sValue )
{
return new MyString( sValue );
}
}//Binding data
ArrayList a = new ArrayList();
a.Add( (MyString)"abcd");
a.Add( (MyString)"东南西北");
a.Add( (MyString)"恭喜发财");
a.Add( (MyString)"你说什么");
a.Add( (MyString)"abcdzzzzzzzzz");dataGrid1.DataSource = a;
DataGridTableStyle tStyle = new DataGridTableStyle();
tStyle.MappingName = "ArrayList";
DataGridTextBoxColumn col = new DataGridTextBoxColumn();
col.MappingName = "Value";
col.HeaderText = "Value";
col.ReadOnly = true;
tStyle.GridColumnStyles.Add( col );dataGrid1.TableStyles.Add( tStyle );