0
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="5" />               
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>   
<DataGrid x:Name="DtgEntity" Grid.Row="0" ItemsSource="{Binding Entities, Mode=TwoWay}" AutoGenerateColumns="False" ></DataGrid>
<GroupBox Header="Selected Entities" Grid.Row="2" >
<DataGrid x:Name="DtgSelectedEntity" ItemsSource="{Binding SelectedEntities, Mode=TwoWay}" ></DataGrid>
</GroupBox>
</Grid>

This is my grid control code. Column Name, Number of columns are changed based on object type. So here i can't specify the grid columns tag. But first column of the grid always check box.

How can i change the first column header as check box. I want to implement select all option for first column of the grid.

Siva G
  • 98
  • 8
  • Implement first column is checkbox column and implement select all is quite easy. But each case have it own way. If you can explain more clear, include full XAML code of your DataGrid, I can help you. – NoName Mar 01 '16 at 04:44
  • I added my full XAML code. Data grid columns count are not fixed. Column count and names changed based on user selection. But first columns must be check box. After select one item its moved to second grid. Single selection working fine. But i need to implement select all option. – Siva G Mar 01 '16 at 05:02
  • your Datagrid bind to a collection contains many type of object or to many collection, each contains one type of object? And does the Property you bind to first column have same name and type? – NoName Mar 01 '16 at 05:44
  • public ObservableCollection Entities – Siva G Mar 01 '16 at 06:05
  • Okay. Seem to delicate if I ask you unveil more. So, just use DataGridCheckBoxColumn, change header template, bind to checkAll_Property. Easily! – NoName Mar 01 '16 at 06:16

1 Answers1

1

Number of columns are changed based on object type. So here i can't specify the grid columns tag

Why not? you can specify Columns tag with a DataGridCheckBoxColumn and have AutoGenerateColumns set to true to generate columns based on object type. That will mean that a checkbox column will always apear and the rest of the columns will be generated by the binded ItemsSource type

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="5" />               
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>   
<DataGrid x:Name="DtgEntity" Grid.Row="0" ItemsSource="{Binding Entities, Mode=TwoWay}" AutoGenerateColumns="False" ></DataGrid>
<GroupBox Header="Selected Entities" Grid.Row="2" >
<DataGrid x:Name="DtgSelectedEntity" ItemsSource="{Binding SelectedEntities, Mode=TwoWay}" AutoGenerateColumns="True" >
    <DataGrid.Columns>
         <DataGridCheckBoxColumn x:Name="myCheckBoxColumn"/>
    </DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>

You can also set the FrozenColumnCount property of the DataGrid to 1 to make that column permamnent to scrolling.

As for the select all feature, there are plenty of threads on SO that covers this issue. here's a few:

Datagrid Column header should check / uncheck CheckBox’s state depending upon whether all CheckBoxes of a DataGridView column are checked or unchecked

How to Select All CheckBox of a Column by DataGrid Header CheckBox in WPF DataGrid

c# code for select all checkbox in wpf datagrid

Hope this helps

Community
  • 1
  • 1
Omri Btian
  • 6,499
  • 4
  • 39
  • 65
  • Sorry If there are any syntax errors this was written straight from memory didn't had a chance to test it yet – Omri Btian Mar 01 '16 at 07:30