Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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 :

C#
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") });

        }



C#
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 ??
Posted

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.
   
Comments
Amd_eagle 28-Feb-11 22:33pm
   
I am writing it in a Silverlight Application so I am not able to use DataTable.
Amd_eagle 28-Feb-11 22:34pm
   
I am writing the datagrid in a silverlight application so i not able to use the DataTable.
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.
   
Comments
Amd_eagle 1-Mar-11 0:28am
   
how do i go about doing it ??
Abhinav S 1-Mar-11 1:30am
   
something like
list.add(new model());
where model is your model class.
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.
   
DataClass obj=new DataClass(); // this is the class used in databinding

observablecollectionobj[0] = obj ; // if want to insert at any position, you have mention the index no.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900