1

I am trying to add some data inside my DataGrid.

I added some columns with the designer. Now I want to add rows with data inside the DataGrid.

Here's my code so far:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var dataContext = new PurchaseOrderDataContext();
    var purchaseOrderTable = dataContext.GetTable<PurchaseOrder>();

    var query = from a in purchaseOrderTable
            select a;

    var purchaseOrders = query;

    foreach (var purchaseOrder in purchaseOrders)
    {
        // I believe that this method is the right one, but what do I pass to it?
        // dataGrid1.Items.Add(test);
    }
}

All I want to know is: What kind of object do I need to use to add something in the DataGrid, and what kind of object do I need to pass to that last method? Also how do I add, let's say, text to a certain column of a row I added?

Thanks a lot!

2 Answers 2

2

Try this:

dataGrid1.ItemsSource = query;
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but ideally if you set binding correctly to a proper source, this shouldn't be needed.
1

In general, you would bind the ItemsSource of the grid to a collection that supports change notification (IObservableCollection is idea) and just add to the collection. If the collection supports change notification, the grid will automatically display the new row.

1 Comment

I'm kind of a newbie with all that stuff, I used to do Mac Programming (Objective-C) so I'm kind of lost right now. I know that I can bind a collections to my ItemsSource. I tried here's what I did: I created SQL Database, linked it to my code with LINQ to SQL classes so now I can play with my database in the code. But in the designer when I try to bind my table to the DataGrid my collection isn't shown in the Binding panel (ItemsSource). Any idea or straightforward guide to this? Thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.