如何动态地在WPF中向datagrid添加行? [英] How do I add row to datagrid in WPF dynamically?

查看:140
本文介绍了如何动态地在WPF中向datagrid添加行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有WPF的数据网格,名称为dgSchedule。我已经在datagrid中有行。我想动态地向数据网格添加新行。



我试过以下但数据网格中没有显示新行。



什么我试过了:



Hello,

I have datagrid of WPF with the name dgSchedule.I already have rows in datagrid.I want to add new row dynamically to the datagrid.

I have tried following but new row wasn't displayed in datagrid.

What I have tried:

ObservableCollection<states> stlist = new ObservableCollection<states>();
foreach (States row in dgSchedule.Items)
    stlist.Add(row);

stlist.Add(s);//Where s is empty list
dgSchedule = null;
dgSchedule = new DataGrid();
dgSchedule.ItemsSource = stlist;

推荐答案

您好,只需指定类型为 DataTable的属性 DataGrid 作为 DataContext ,为此属性实现 INotifyPropertyChanged 。现在只需在表格中创建一个新行,它将反映在数据网格上。
Hi, Just assign a property of type DataTable to the DataGrid as DataContext, implement INotifyPropertyChanged for this property. Now just create a new row in the table, it will reflect on the datagrid.


这篇关于如何动态地在WPF中向datagrid添加行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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