【问题标题】:Complex DataBinding accepts as a data source either an IList or an IListSource复杂 DataBinding 接受 IList 或 IListSource 作为数据源
【发布时间】:2015-05-30 08:17:22
【问题描述】:

我想将组的值绑定到 DataTable dt1=new DataTable()。之后,我想将 DataTable 数据绑定到 DataGrid。但我无法做到。当我将数据源作为组直接提供给 datagrid 时,我得到了“Complex DataBinding 接受 IList 或 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;

    }

【问题讨论】:

    标签: c# list datatable dataset enumeration


    【解决方案1】:

    您可以像这样将dt 绑定到DataGrid

    dataGrid1.DataContext = dt.DefaultView;
    

    在您的 XAML 中:

    <DataGrid x:Name="dataGrid1" ItemsSource={Binding}"/>
    

    DefaultView 是一个DataView,它实现了IList

    就您的其余代码而言,我不知道您在做什么。例如,这行的目的是什么:

    DataTable dt1 = new DataTable();
    

    你对dt1什么都不做。

    【讨论】:

    • 感谢您的回答,我想将组的值添加到 dt1 中,然后我想将 dataGrid 数据源的值提供给 dt1
    • 我想将日期的不同值过滤到组中,组的结果想要分配 DataTable dt1。之后,我可以将 dt1 的记录作为数据源提供给 dataGrid。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多