Ray Dean

Ray Dean

  • NA
  • 8
  • 8.8k

WPF - Making a checkbox select rows within a data grid

Dec 4 2020 12:31 PM
Hi All.
 
New to C# programming, I set myself a challange to build a GUI application. The application populates a datagrid with data from a table. I have a checkbox added to the datagrid column, and what I would like is for the checkbox to be the sole method for selecting items within the datagrid. So if the checkbox is checked 10 times for 10 datagrid items selected, those items are selected from the datagrid. This is what I have so far and I am stuck with the checkbox issue.
 
As can be seen below, despite selecting server id 7,8 the datagrid row only recognises 3 as its the only one highlighted.
 
 
  1. <DataGrid x:Name="GridServerList" CanUserAddRows="False" AlternatingRowBackground="Beige" Grid.Column="5" HorizontalAlignment="left" Height="365" Width="625" AutoGenerateColumns="False" Margin="9,159,0,0" VerticalAlignment="Top" Grid.ColumnSpan="8" SelectedItem="{Binding PopulateDataGrid, Mode=TwoWay}" >  
  2. <DataGrid.Columns>  
  3. <DataGridCheckBoxColumn x:Name="chkSelectedServer" Binding="{Binding IsChecked, Mode=TwoWay}" Header="Choose" IsReadOnly="False" CanUserSort="False">  
  4. <DataGridCheckBoxColumn.ElementStyle>  
  5. <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}" >  
  6. <EventSetter Event="Checked" Handler="OnChecked" />  
  7. </Style>  
  8. </DataGridCheckBoxColumn.ElementStyle>  
  9. </DataGridCheckBoxColumn>  
  10. <DataGridTextColumn Binding="{Binding Server_Id}" Header="Server_Id" IsReadOnly="true"/>  
  11. <DataGridTextColumn Binding="{Binding Server_Name}" Header="Server_Name" IsReadOnly="true"/>  
  12. </DataGrid.Columns>  
  13. </DataGrid>  
Within my CS file, I do not have any code relating to the functionality that I require.

Answers (2)