UWP:如何将任意数据映射到Telerik RadDataGrid

时间:2017-11-21 20:03:59

标签: datagrid uwp telerik

所以我遇到的情况是我收到一个Dictionary(字符串,字符串)条目的集合,其中每个条目的键是列名&重视价值。我想将它们推送到RadDataGrid,以便每个字典映射到一行。如果我知道我提前获得了多少/多少列,我只是将它们映射到一个对象并完成它。不幸的是,每次都可能会有所不同,因此不会起作用。

到目前为止,我没有运气。我已经尝试直接映射*(集合),将其转换为动态对象& XMLDocument,其中没有一个工作。还得到了Fall Creators Update&尝试将它映射到DataTable,也没有运气。

在手动添加列后,我一直在尝试将DataTable的DefaultView映射到网格的ItemsSource,但是当我得到正确的列和标题时,我仍然没有'得到字段值。不确定下一步该去哪里。

请注意,我没和Telerik结婚。如果其他人知道一个适当可用的UWP数据网格解决方案,它可以让我映射这样的任意数据,我很乐意听到它。

使用标准UWP应用程序的示例:

MainPage.xaml中:

<Page xmlns:my="using:Telerik.UI.Xaml.Controls.Grid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestTelerikDataGrid"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ComponentModel="using:System.ComponentModel" 
    x:Class="TestTelerikDataGrid.MainPage"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,125">
        <my:RadDataGrid Margin="0,0,0,-125" x:Name="dataGrid" AutoGenerateColumns="False" >

        </my:RadDataGrid>
    </Grid>
</Page>

后端:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace TestTelerikDataGrid {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page {

        private ObservableCollection<Dictionary<string, string>> items = new ObservableCollection<Dictionary<string, string>>();

        public ObservableCollection<Dictionary<string, string>> ItemDictionary {
            get {
                return items;
            }
            set {
                items = value;
            }
        }

        public DataTable Items { get; set; }

        public MainPage() {

            this.InitializeComponent();

            CreateItems(); // creates sample data structurally identical to what we'll get in the actual app (i.e., obsv. collection of dictionaries)
            CreateTable(); // attempt to take the collection created above and map it to the RadDataGrid
        }

        private void CreateItems() {
            for (int i = 0; i < 5; i++) {
                Dictionary<string, string> row = new Dictionary<string, string>();
                row["A"] = "A" + i.ToString();
                row["B"] = "B" + i.ToString();
                row["C"] = "C" + i.ToString();

                ItemDictionary.Add(row);

            }
        }

        private void CreateTable() {
            Items = new DataTable();

            if (ItemDictionary.Count == 0) return;

            foreach (KeyValuePair<string, string> entry in ItemDictionary[0]) {
                DataColumn column = new DataColumn(entry.Key);
                Items.Columns.Add(column);
                Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn dgc = new Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn();
                dgc.Name = entry.Key;
                dgc.Header = entry.Key;
                dgc.PropertyName = entry.Key;
                dataGrid.Columns.Add(dgc);
            }

            foreach (Dictionary<string, string> rowEntry in ItemDictionary) {
                DataRow row = Items.NewRow();
                int col = 0;
                foreach (KeyValuePair<string, string> entry in rowEntry) {
                    row[entry.Key] = entry.Value;
                }
                Items.Rows.Add(row);
            }
            DataView dv = Items.DefaultView;
            dataGrid.ItemsSource = dv;
        }
    }
}

理想情况下,这将产生一个包含5行,3列(A,B,C)的表格以及显示正确值的字段(例如,第一行读取A0,B0,C0)。

2 个答案:

答案 0 :(得分:0)

  

我一直在尝试在手动添加列之后将DataTable的DefaultView映射到网格的ItemsSource,但是当我得到正确的列和标题时,我仍然没有得到字段值。不确定下一步该去哪里。

     

这些都是Fall Creators Update附带的UWP 2.0的一部分。我相信这是建立16299或类似的东西。对不起,应该提到我正在使用的构建。

RaDataGrid不支持直接将DataTable或DataView设置为ItemsSource。您需要转换为IEnumerble集合而不是DataView。

请在Telerik论坛上查看此主题以获取更多详细信息:binding-dictionary-to-raddatagrid

答案 1 :(得分:0)

最后,我结束了创建一个通用&#34; GridRow&#34;具有属性的类&#34; Item00&#34; ...&#34; Item99&#34;并且只是将数据映射到那个。它是一种药丸,但它有效。把它放在这里为下一个人。

public class GridRow {
        public Int32 Index { get; set; }
        public string Item00 { get; set; }
        public string Item01 { get; set; }
        public string Item02 { get; set; }
...etc
}

public ObservableCollection<GridRow> GridData { get; set; }

以下是您填写的方式:

 GridData = new ObservableCollection<GridRow>();
            foreach (Dictionary<string, string> record in ViewModel.ItemsSource) {
                GridRow gridRow = new GridRow();
                gridRow.Index = rowIndex;
                colIndex = 0;
                foreach (DataGridHeader header in ViewModel.Headers) {
                    gridRow.GetType().GetProperty(string.Format("Item{0:D2}", colIndex)).SetValue(gridRow, record[header.Name]);
                    colIndex += 1;
                }
                GridData.Add(gridRow);
                rowIndex += 1;
            }

你明白了。

相关问题