Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all

XML
<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

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[^]
 
Share this answer
 
v3
XML
<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 .
 
Share this answer
 
Comments
Member 11437473 21-Feb-15 2:10am    
How to read the check box status in c# for above code.
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;
            }
        }
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900