Add row by row to the datagrid

Dec 15, 2008 at 11:07 AM
Edited Jan 6, 2009 at 6:02 AM
Hi,

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 12:00 PM
Hi,

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:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

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

Regards,
Colin E.
Dec 15, 2008 at 12: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 12:22 PM
Hi,

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!
Coordinator
Dec 16, 2008 at 1:22 AM
There was a similar thread about this awhile back, http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=34065.  I don't know if it will help you though.  Items.Add should work.  Could you show a code snippet.
Dec 17, 2008 at 12: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?

Thanks!
Coordinator
Dec 17, 2008 at 12:43 PM
Try ds.Tables[0].DefaultView.
Dec 17, 2008 at 12:53 PM
Or,

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

Regards,
Colin E.
--
http://wpfadventures.wordpress.com/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples

Dec 17, 2008 at 12: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 7:21 PM
This was asked on the MSDN forums:

"Difference(s) between ItemsSource and DataContext":
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9bfdb32c-a4ce-46ad-b977-e12d51e815e2/


Regards,
Colin E.
--
http://wpfadventures.wordpress.com/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples