0
Answer

How to get column values of selected columns in datagrid wpf

Abhinav

Abhinav

10y
2k
1

I have a Listbox in which I have added two controls i.e a Checkbox and a Datagrid side by side as a Listboxitem. I have done it in a following manner:

In Code behind: listBox1.items.Add(dt)

here dt is the table name.

In Xaml:

<ListBox Grid.Row="0" AlternationCount="2" MinHeight="305" HorizontalAlignment="Stretch" Name="listBox1" VerticalAlignment="Stretch" MinWidth="537" >
  <ListBox.ItemTemplate>
  <DataTemplate>
  <StackPanel Name="stkPanel" MinHeight="80" MinWidth="500" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  <Grid>
  <Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
 <ColumnDefinition Width="Auto"/>
 </Grid.ColumnDefinitions>
 <DataGrid Grid.Column="1" AutoGenerateColumns="True" MinHeight="75" HorizontalAlignment="Center" Name="dataGrid1" VerticalAlignment="Stretch" MinWidth="470" MaxWidth="900" ItemsSource="{Binding}" IsReadOnly="True" Background="#E6BAB7B7">
 <DataGrid.ColumnHeaderStyle>
 <Style TargetType="DataGridColumnHeader">
 <Setter Property="ContentTemplate">
 <Setter.Value>
 <DataTemplate>
  <CheckBox Name="colCheckBox" IsChecked="{Binding IsChecked, ElementName=IsDone, Mode=OneWay}" Content="{Binding}"/>
 </DataTemplate>
 </Setter.Value>
 </Setter>
 </Style>
 </DataGrid.ColumnHeaderStyle>
 </DataGrid>
<CheckBox Grid.Column="0" Height="35" Width="25" Name="IsDone" HorizontalAlignment="Right" VerticalAlignment="Center" Checked="IsDone_Checked" />
 </Grid>
 </StackPanel>
 </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

Suppose for first listboxitem, there is "IsDone" checkbox and a datagrid. If I select "IsDone" check box (listbox item) all the datagrid column header check box is selected.Now, I can uncheck some of the datagrid coulmn header check boxes.What I want is to get values of the entire column only whose columnheader check box is checked and bind those values to a datatable.Similarly repeat the above process for other listbox item.Can you please suggest me how to do that in wpf?