private void button2_Click(object sender, EventArgs e)
        {
            string[] citys = { "juzi","mako","pingguo","aaa"};
            IEnumerable<string> place = from city in citys where city.Length>2 select city;
            this.dataGridView1.DataSource = dt;
        }
就这个代码,同样的代码在web中可以显示,   但是在winform中无法显示,    难道winform中必须需要把他转化成datatable吗? 如果需要怎么转化? 能否不转化?

解决方案 »

  1.   

    你的dt是什么阿,datagridview的datasource是object类型的,不是非要转换成datatable.
      

  2.   

    DataGridView的DataSource属性为object类型
    而LINQ查询结果为IEnumerable<T>或IQueryable<T>类型,如果直接作为数据源绑定到DataGridView,将无法显示任何内容。Enumerable类为IEnumerable<T>接口定义了一系列扩展方法,其中的ToList<T>方法可以将IEnumerable<T>转换为List<T>。
    var test = new List<string> { "a", "b", "c" };
    dataGridView1.DataSource = (from s in test select new { s }).ToList();