将行添加到DataGrid Silverlight [英] Adding Row to DataGrid Silverlight

查看:96
本文介绍了将行添加到DataGrid Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Datagrid来显示来自数据库的数据。

每次我从我的数据花瓶加载数据时,旧的数据被收集,并被新的数据替换。



我想要的是保留前一行并将新数据添加到数据网格的末尾。



我的代码如下所示:



  public  MainPage( )
{
InitializeComponent();
dataGrid1.Columns.Add( new DataGridTextColumn {Header = Year,Binding = new System.Windows.Data.Binding( )});
dataGrid1.Columns.Add( new DataGridTextColumn {Header = 正在运行,Binding = new System.Windows.Data.Binding( 一个)});
dataGrid1.Columns.Add( new DataGridTextColumn {Header = Idle,Binding = new System.Windows.Data.Binding( 两个)});
dataGrid1.Columns.Add( new DataGridTextColumn {Header = 关闭,Binding = new System.Windows.Data.Binding( )});

}







  void  client_DoWorkCompleted( object  sender,DoWorkCompletedEventArgs e)
{

dataGrid1。 ItemsSource = list;

}





如何从数据库中追加新数据而不是覆盖数据? div class =h2_lin>解决方案

将数据保存在DataTable对象中,然后将网格绑定到它。获取新记录后,将其添加到DataTable并刷新绑定。


将新项添加到数据源集合中,即 list



如果您已正确完成绑定,则会自动将一行添加到数据网格。


创建全局声明

ObservableCollection< person> myObs = new ObservableCollection< person>();





添加新功能



Private void AddRows(){

myObs.Add(new Person(){});

datagrid1.itemsource = myObs;

}



点击按钮调用上面的函数动态添加新行。


I have a Datagrid which show data from a database.
each time i load the data from my data vase the old data is earsed and the is replaced with the new data.

What i want is for the pervious row to remain and the new data to be appened to the end of the datagrid.

my code is as shown below :

public MainPage()
        {
            InitializeComponent();
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Year", Binding = new System.Windows.Data.Binding("year") });
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Running", Binding = new System.Windows.Data.Binding("One") });
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Idle", Binding = new System.Windows.Data.Binding("Two") });
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Turned Off", Binding = new System.Windows.Data.Binding("Three") });

        }




void client_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {

                       dataGrid1.ItemsSource = list;

        }



how do i append the new data from the database rather than overwriting the data ??

解决方案

Keep your data in a DataTable object, then bind the grid to it. When you get a new record, add it to the DataTable and refresh the binding.


Add a new item to your datasource collection i.e. list.

If you have done your binding correctly, a row will get added automatically to the datagrid.


Create an global declaration
ObservableCollection<person> myObs = new ObservableCollection<person>();


Add New Function

Private void AddRows(){
myObs.Add(new Person() {});
datagrid1.itemsource=myObs;
}

Call the above function in a button click to add new rows dynamically.


这篇关于将行添加到DataGrid Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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