如何定义数据表,以便在映射到datagrid时List <string>字段将展开?

时间:2016-02-12 23:21:11

标签: c# wpf datagrid dataview

似乎最容易解决的问题最容易解决。

我正在编写一些代码来从Amazon Web Services转储数据。我收集数据并将其存储在数据表中。

    public static DataTable GetEC2DetailsTable()
    {
        DataTable table = new DataTable();
        // Here we create a DataTable .
        table.Columns.Add("AccountID", typeof(string));
        table.Columns.Add("InstanceID", typeof(string));
        table.Columns.Add("SecurityGroups", typeof(List<string>));

        //This code ensures we croak if the InstanceID is not unique.  How to catch that?
        UniqueConstraint makeInstanceIDUnique =
            new UniqueConstraint(new DataColumn[] { table.Columns["InstanceID"] });
        table.Constraints.Add(makeInstanceIDUnique);

        //Can we set the view on this table to expand IEnumerables?
        DataView mydataview = table.DefaultView;

        //????Can we define the field here somehow?

        return table;
    }

在我的程序中,我有一个填充其中一个表的函数,我想直接将其转储到WPF DataGrid中进行显示。下面,datable返回我的新数据表。

            var datable = Scanner.GetEC2Instances(ProfilesComboBox.SelectedItem.ToString(), RegionListcomboBox.SelectedItem.ToString());
            DasGrid.ItemsSource = datable.DefaultView;

除了数据类型为字符串列表的列外,这种方法很有效。对于这些列,数据仅在Datagrid中显示为(集合)。

我已经看过编写XAML代码的各种示例,但我更愿意将逻辑构建到数据表中。我计划将其他数据表转储到网格,所以不要污染它。从理论上讲,我应该可以编辑默认的TableView来使这个工作,不管我吗?试图解决这个问题。在我真正开始削减之前的最后一道障碍。

0 个答案:

没有答案