Tommy B.
Tommy B.

Reputation: 3659

Adding data to WPF DataGrid

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!

Upvotes: 1

Views: 2024

Answers (2)

grantnz
grantnz

Reputation: 7423

Try this:

dataGrid1.ItemsSource = query;

Upvotes: 2

RQDQ
RQDQ

Reputation: 15579

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.

Upvotes: 1

Related Questions