Click here to Skip to main content
14,608,056 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all

<DataGrid AutoGenerateColumns="False" Height="141" HorizontalAlignment="Left" Margin="114,109,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="353" ItemsSource="{Binding}" >
<DataGrid.Columns>
    <DataGridTextColumn Header="Alias" Binding="{Binding ItemAdAlias}" Width="50"/>
  <DataGridTextColumn Header="Min Stock" Binding="{Binding ItemAdMin}" Width="60"/>
 <DataGridTextColumn Header="Max Stock" Binding="{Binding ItemAdMax}"/>
 <DataGridTextColumn Header="Current Stock" Binding="{Binding ItemAdCurr}"/>
 <DataGridTextColumn Header="Color" Binding="{Binding ItemAdColor}"/>
 <DataGridTextColumn Header="Set" Binding="{Binding ItemAdSet}"/>
 <DataGridTextColumn Header="Remarks" Binding="{Binding ItemAdRemarks}"/>
</DataGrid.Columns>
                   
   </DataGrid>


This is my datagrid my application.
now i have to add another row with checkboxes corresponding to each column and on check and uncheck save and fetch the value from the database.

Please tell me how to do this..
or any other alternate idea will be helpful.

Thank you
Posted
Updated 23-Oct-17 4:16am
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can use the DataGridCheckBoxColumn or a DataGridTemplateColumn with a CheckBox inside. Check these links to get an idea how to do that.
adding the checkbox column in to WPF datagrid and select the checked rows [^]
Add checkbox in WPF Datagrid DatagridTemplateColumnHeader[^]
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

<wpf:DataGridTemplateColumn Width="SizeToHeader" IsReadOnly="True" Header="First Selection">
    <wpf:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=First}"  Checked="OnChecked" Margin="2,0,2,0" HorizontalAlignment="Center" VerticalAlignment="Stretch" />
        </DataTemplate>
    </wpf:DataGridTemplateColumn.CellTemplate>



You can use template column to add checkbox in datagrid .
   
Comments
Member 11437473 21-Feb-15 2:10am
   
How to read the check box status in c# for above code.
Rate this:
Please Sign up or sign in to vote.

Solution 3

I went with very simple..

<DataGridCheckBoxColumn Binding="{Binding IsChecked, Mode=TwoWay}" IsReadOnly="False">
                                <DataGridCheckBoxColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <CheckBox Checked="FieldDataGridChecked" Unchecked="FieldDataGridUnchecked" Margin="2" />
                                    </DataTemplate>
                                </DataGridCheckBoxColumn.HeaderTemplate>
                            </DataGridCheckBoxColumn>


Just handle the checked and unchecked events, and apply the "checked" to the model.

private void FieldDataGridChecked(object sender, RoutedEventArgs e)
        {
            foreach (FieldViewModel model in _fields)
            {
                model.IsChecked = true;
            }
        }

        private void FieldDataGridUnchecked(object sender, RoutedEventArgs e)
        {
            foreach (FieldViewModel model in _fields)
            {
                model.IsChecked = false;
            }
        }
   
Comments
CHill60 23-Oct-17 17:47pm
   
However, over 4 years ago the OP went with solution 1. Stick to answering new questions where the OP still needs help
pccoder71 5-Jan-18 10:05am
   
yes, he did, but that doesn't mean my example won't help others.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100