如何将数据添加到数据网格WPF编程 [英] How to add Data to a WPF datagrid programatically

查看:105
本文介绍了如何将数据添加到数据网格WPF编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加数据项目在WPF中的DataGrid 编程不具备的绑定?在的DataGrid 有4列。


解决方案

这不是很清楚,你喜欢做什么。我想,你已经定义了一些地方,你想要把DataGrid中。为了便于说明,我创建了一个新的WPF项目,并使用由chridram,谁张贴的第一个答案提供的代码。



在以下MainWindow.xaml我的名字网格MainGrid访问它后面的代码:



<预类=郎咸平的XML prettyprint-覆盖> <窗​​口x:类=WpfExperiments.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=主窗口HEIGHT =350WIDTH =525>
<电网NAME =MainGrid/>
< /窗GT;



的DataItem类不是一个WPF类的,而是由自己创建的自定义类:



<预类=郎-CS prettyprint-覆盖> 公共类的DataItem
{
公共字符串列1 {搞定;组; }
公共字符串列2 {搞定;组; }
公共字符串栏3 {搞定;组; }
公共字符串Column4 {搞定;组; }
}

要允许存储的DataItem DataGrid中显示的数据编程的对象,您可以这样做以下内容:

 公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();

//你的程序创建的DataGrid被连接到这里MainGrid
变种DG =新的DataGrid();
this.MainGrid.Children.Add(DG);

//这里创建四列具有相同的名称的DataItem的属性
的for(int i = 1; I< = 4; ++ I)
{
VAR列=新DataGridTextColumn();
column.Header =列+我;
column.Binding =新的绑定(列+ I);
dg.Columns.Add(列);
}

//创建并添加假数据的两行被显示,这里
dg.Items.Add(新的DataItem {列1 =A.1,列2 =A.2,栏3 =A.3,Column4 =A.4});
dg.Items.Add(新的DataItem {列1 =B.1,列2 =B.2栏3 =B.3,Column4 =B.4});
}
}



我希望这有助于。



问候
约尔格


How can I add data Items to a DataGrid programmatically in WPF which do not have bindings? The DataGrid has 4 columns.

解决方案

It is not very clear, what You like to do. I guess, You have defined some place where You want to put the DataGrid. For illustration purposes, I created a new WPF project and use the code provided by chridram, who posted the first answer.

In the following MainWindow.xaml I name the Grid MainGrid to access it in the code behind:

<Window x:Class="WpfExperiments.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 Name="MainGrid"/>
</Window>

The DataItem class is not a WPF class, but a custom class created by Yourself:

public class DataItem
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
}

To let the DataGrid display data stored in DataItem objects programmatically, You may do the following:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Your programmatically created DataGrid is attached to MainGrid here
        var dg = new DataGrid();
        this.MainGrid.Children.Add(dg);

        // create four columns here with same names as the DataItem's properties
        for (int i = 1; i <= 4; ++i)
        {
            var column = new DataGridTextColumn();
            column.Header = "Column" + i;
            column.Binding = new Binding("Column" + i);
            dg.Columns.Add(column);
        }

        // create and add two lines of fake data to be displayed, here
        dg.Items.Add(new DataItem { Column1 = "a.1", Column2 = "a.2", Column3 = "a.3", Column4 = "a.4" });
        dg.Items.Add(new DataItem { Column1 = "b.1", Column2 = "b.2", Column3 = "b.3", Column4 = "b.4" });
    }
}

I hope this helps.

Greetings Jörg

这篇关于如何将数据添加到数据网格WPF编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆