DataBinding接受IList或IListSource作为

DataBinding接受IList或IListSource作为

本文介绍了Complex DataBinding接受IList或IListSource作为数据源。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to bind the value of groups into DataTable dt1=new DataTable(). After that I want to bind the DataTable datat to DataGrid. But I am unable to do it. When i given the datasource to datagrid as groups directly then I got Exception of "Complex DataBinding accepts as a data source either an IList or an IListSource."










private void BindGrid()
        {
            var dt = new DataTable();
            dt.Columns.Add("Date",typeof(string));
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("City",typeof(string));
            dt.Columns.Add("Mobile",typeof(string));
            dt.Rows.Add("1/11/2014", "David", "Noida", "Bsnl");
            dt.Rows.Add("1/11/2014", "James", "Mumbai", "Airtel");
            dt.Rows.Add("30/1/2015", "Ramesh", "Pune", "Vodafone");
            dt.Rows.Add("30/1/2015", "Kamal", "Kolkata", "Idea");
            dt.Rows.Add("15/5/2015", "Mahesh", "Chennai", "Reliance");
            var groups = (
            from DataRow row in dt.AsEnumerable()
            select new
            {
                date = row.Field<string>("Date")
            }
            ).Distinct();

            DataTable dt1 = new DataTable();
            
            dataGrid1.DataSource = groups;

        }</string>

推荐答案

DataTable dt = new DataTable();
dt.Columns.Add("Date",typeof(DateTime));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("City",typeof(string));
dt.Columns.Add("Mobile",typeof(string));

dt.Rows.Add(new object[]{new DateTime(2014,11,1), "David", "Noida", "Bsnl"});
dt.Rows.Add(new object[]{new DateTime(2014,11,1), "James", "Mumbai", "Airtel"});
dt.Rows.Add(new object[]{new DateTime(2015,1,30), "Ramesh", "Pune", "Vodafone"});
dt.Rows.Add(new object[]{new DateTime(2015,1,30), "Kamal", "Kolkata", "Idea"});
dt.Rows.Add(new object[]{new DateTime(2015,5,15), "Mahesh", "Chennai", "Reliance"});

var days = dt.AsEnumerable().Select(a=>a.Field<datetime>("Date")).Distinct().ToList();

dataGrid1.DataSource = days;</datetime>





对象的类型是列表< DateTime> 。它应该解决你的问题;)



days object is type of List<DateTime>. It should resolve your issue ;)


这篇关于Complex DataBinding接受IList或IListSource作为数据源。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:07