Cygu
Cygu

Reputation: 363

Adding a checkbox-column to a WPF-datagrid which is linked to a datatable

I have a problem adding a checkbox-column to a WPF-datagrid.

  1. I create a datatable with 8 columns and fill it using sql.
  2. I bind the datatable to the WPF-datagrid using ItemsSource like:

    XAML:

    <DataGrid x:Name="dgOPListEntries" Grid.Row="1" Grid.ColumnSpan="2" Margin="5" ItemsSource="{Binding}" AutoGenerateColumns="True"></DataGrid>

    CodeBehind:

    dgOPListEntries.ItemsSource = dtOPListEntries.DefaultView;

So far, this works fine.

I now want to add a checkbox-column to the datagrid where I can select some rows. (The selected rows will then be updated on the sql server.)

Any help is appreciated.

Upvotes: 0

Views: 2852

Answers (1)

Alex Skiba
Alex Skiba

Reputation: 437

If what you want is just to select some rows, you can use DataGrid.SelectionMode and DataGrid.SelectionUnit properties set to DataGridSelectionMode.Extended and DataGridSelectionUnit.FullRow, respectively.

In addition, DataGrid creates checkbox column automatically for data of type bool, if you can add a field to your items source.

Upvotes: 2

Related Questions