收集到DataTable

时间:2015-04-13 15:20:55

标签: c# gridview collections datatable converter

我正在尝试将Collection转换为DataTable,以便我可以将它绑定到gridview。 (如果还有另一种方法,我愿意接受这些想法)

这就是集合的样子:

public class CustomerCollection 
{
    [JsonProperty("customer")] 
    public IEnumerable<customer> Customer { get; set; } 
}

public class customer  
{ 
    [JsonProperty("customer-name")] 
    public string strCustomerName { get; set; } 
    [JsonProperty("customer-address")] 
    public List<string> lCustomerAddress { get; set; } 
}

最终结果如下:

Collection

尝试将其绑定到gridview时会出现问题。我得到的错误如下:

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

当使用Helper类尝试将其转换为DataTable时,我得到:

The type arguments for method 'CollectionHelper.ConvertTo<T>(System.Collections.Generic.IList<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

1 个答案:

答案 0 :(得分:1)

您应该将网格视图绑定到Customer集合属性:

gvDisplay.DataSource = customers.Customer;

您的代码无法正常工作的原因是您将其直接绑定到类型为customers的{​​{1}}变量,而CustomerCollection变量(尽管其名称)不是集合完全,它只是一个带有Customer属性的简单.NET类。

相关问题