将数据从列表写入数据网格

时间:2012-07-07 18:14:22

标签: c# wpf datagrid xceed-datagrid

我的数据网格包含字段idstickervolatilityrate of return。我还有一个结构container,其中包含字段idstickervolatilityrateofreturn

   struct container
    {
        public container(string sticker, decimal volatility, decimal rateofreturn)
        {
            this.sticker = sticker;
            this.volatility = volatility;
            this.rateofreturn = rateofreturn;
        }
        string sticker;
        decimal volatility;
        decimal rateofreturn;
    };

我使用结构创建列表并添加值

container cnt = new container(x.Trim(), volatility, ror);
list.Add(cnt);

但是如何从列表中将这些值写入datagrid?当我这样做时

dataGridControl1.ItemsSource = list;

它创建的行数与列表的计数完全相同,但它们都是空的。

xaml中的datagrid定义

<xcdg:DataGridControl Height="392" Margin="0,190,534,0" Name="dataGridControl1" VerticalAlignment="Top" AutoCreateColumns="False" ItemsSource="{Binding}" AllowDetailToggle="False">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column Title="Sticker" ReadOnly="True" FieldName="Sticker"/>
            <xcdg:Column Title="Rate of return" ReadOnly="True" FieldName="Rate of return"/>
            <xcdg:Column Title="Volatility" FieldName="Volatility" ReadOnly="True"/>
        </xcdg:DataGridControl.Columns>
        <xcdg:DataGridControl.View>
            <xcdg:TableflowView>
                <xcdg:TableflowView.Theme>
                    <xcdg:LunaHomesteadTheme />
                </xcdg:TableflowView.Theme>
            </xcdg:TableflowView>
        </xcdg:DataGridControl.View>
    </xcdg:DataGridControl>

1 个答案:

答案 0 :(得分:1)

您必须添加公共属性。

XAML文件:

<Window x:Class="DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="dgData" AutoGenerateColumns="True" />


    </Grid>
</Window>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<container> _source = new List<container>();

            for (int i = 0; i < 10; i++)
            {
                _source.Add(new container("test", 1 * 10, 1 * 10000));
            }

            dgData.ItemsSource = _source;
        }
    }

    public struct container
    {
        public container(string sticker, decimal volatility, decimal rateofreturn)
        {
            this.sticker = sticker;
            this.volatility = volatility;
            this.rateofreturn = rateofreturn;
        }
        string sticker;
        decimal volatility;
        decimal rateofreturn;

        public string Sticker { get { return sticker; } set { sticker = value; } }
        public decimal Volatility { get { return volatility; } set { volatility = value; } }
        public decimal Rateofreturn { get { return rateofreturn; } set { rateofreturn = value; } }
    };
}