listBox是可以的:
StringCollection a = new StringCollection();
a.Add("abcd");
a.Add("东南西北");
a.Add("恭喜发财");
a.Add("你说什么");
a.Add("abcdzzzzzzzzz");

listBox1.DataSource = a;但是换成DataGrid,只绑定显示了字符串的 lengh 属性,和 listBox 最关键的区别在哪里呢?

解决方案 »

  1.   

    可以绑定呀,很正常:
    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();}
      

  2.   

    楼上:
    代码没有出错,但是效果不对!我写了:“只绑定显示了字符串的 lengh 属性”
      

  3.   

    在winform环境下造成这个现象的原因,是因为通过IList或者其他Icollection之类的绑定datagrid的时候,是根据item的公共属性来显示。也就是说对于StringCollection的每个Item其实就是一个string对象,而对于string对象来说Length是它的公共属性,所以在绑定的时候直接体现出来了。
      

  4.   

    所以要做如下的转换:
    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 );