Add row by row to the datagrid

Dec 15, 2008 at 12:07 PM
Edited Jan 6, 2009 at 7:02 AM

This is a very basic question, but I couldn't see any method to add rows one by one. In my program I am going to create the columns first and then add rows on demand. I could add the header :- )   but still couldn't find how to add rows on demand. If you can provide a link to a sample that would be great.

Thank you!
Dec 15, 2008 at 1:00 PM

I would strongly recommend creating a list of objects that represent the data which you use to populate your data grid, then use data binding to bind the properties of these objects to your grid.

The source code for this article demonstrates how to bind to a collection of objects:

(look at the validation examples for how to bind to simple objects rather that DataSet / DataTables)

Colin E.
Dec 15, 2008 at 1:03 PM
I can see two alternatives: either you work with the DataGrid's "Items" property directly (leaving the DataGrid's properties relating to data binding unset), or you bind the DataGrid control to an ObservableCollection<> and manipulate that (useful if you want to add custom logic to your collection, for example). In either case, you would probably need to implement a business object with the relevant properties (even if only by a simple {get;set;} pair) and data bind the columns to its properties.

I have no direct experience working directly with the Items collection in DataGrid since I work exclusively with data-bound scenarios, but if all you want is to display information in a grid, I don't see why that wouldn't work. And if you need access to the properties, you can always cast an item back to your business object type.
Dec 15, 2008 at 1:22 PM

I am receiving some data time to time. There is not easy way to create a collection/object _without_ using reflection.  

Anyway I'll have to add row by row. I tried Items.Add but doesn't seem to be working.... Is there any example?

Thank you!
Dec 16, 2008 at 2:22 AM
There was a similar thread about this awhile back,  I don't know if it will help you though.  Items.Add should work.  Could you show a code snippet.
Dec 17, 2008 at 1:09 PM
Finally I decided to use DataSet and DataTables. I want to to know how can I set a DataTable to a DataGrid.

I tried the following way but doesn't seem to be working. But as I remember I've used this way a few days ago. Now I am using a diffent set of DataGrid properties, sometimes that may be the reason for this issue. What are the properties I should set if I am going to use following way?

DataGrid.DataContext = ds.Tables[0];

Is there any other way to set a DataTable to the DataGrid?

Dec 17, 2008 at 1:43 PM
Try ds.Tables[0].DefaultView.
Dec 17, 2008 at 1:53 PM

DataGrid.ItemsSource = ds.Tables[0].DefaultView;

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples

Dec 17, 2008 at 1:54 PM
Hi Vinsibal,

Thanks for your reply. I just tried to set dataGrid.ItemSource = DataTable.DefaultView ; this seems to be working. What is the difference between setting the datatable to ItemSource and dataContext.

Actually dataContext doesn't seem to be working with Table.DefaultView!

I have many dataTAbles and one DataGrid. I want to set my DataTAbles on demand. What do you recomand for this?

Thanks Again!
Dec 17, 2008 at 8:21 PM
This was asked on the MSDN forums:

"Difference(s) between ItemsSource and DataContext":

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples