Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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."



C#
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>
Posted
Updated 29-May-15 23:09pm
v2

1 solution

"Complex DataBinding accepts as a data source either an IList or an IListSource" error message means that you have to bind list of object (complex data type) rather than IEnumerable<String>.

Try to change code as follow:
C#
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>


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900