Pochmurnik
Pochmurnik

Reputation: 828

Linq to list, then to DataGrid

I have an Account(accountID, accountName, transactionCount) and I want, using LINQ insert all rows into List and display that list on DataGrid.

I load rows into List:

List<Account> accounts = dataContext.Accounts.ToList();

Now I don't know how to insert that into DataGrid, I predefined DataGrid columns. I can imagine that I'm missing some maping.

Also, maybe I can directly load all table rows into DataGrid (but with predefined columns). But I think that I will need this List option for joining tables.

Here is the XAML code of DataGrid. I tried to use Gamesh tip, but grid remains blank:

<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="164,25,0,0" Name="dataGridAccounts" VerticalAlignment="Top" Width="299">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Account ID" />
        <DataGridTextColumn Header="Account Name" />
        <DataGridTextColumn Header="Transactions Count" />
    </DataGrid.Columns>
</DataGrid>

Upvotes: 0

Views: 125

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

Try the add binding as below.

<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="164,25,0,0" Name="dataGridAccounts" VerticalAlignment="Top" Width="299">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Account ID" Binding="{Binding accountID}" />
            <DataGridTextColumn Header="Account Name" Binding="{Binding accountName}" />
            <DataGridTextColumn Header="Transactions Count" Binding="{Binding transactionCount}" />
        </DataGrid.Columns>
    </DataGrid>

Upvotes: 1

Related Questions