Maverick
Maverick

Reputation: 2022

select the value from a checkbox in wpf data grid

I am trying to select the value from the datagrid checkbox (whether it is checked or unchecked) and second column(UserName). Here is the datagrid code.

     <DataGrid AutoGenerateColumns="False"  Name="enableDataGrid" ItemsSource="{Binding}" Margin="0,0,0,81">
     <DataGrid.Columns>
     <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>                                           
             <CheckBox HorizontalAlignment="Center"/>                                      
          </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
     <DataGridTextColumn Header="User Name" Binding="{Binding Path=uName}"/>
     </DataGrid.Column>
     </DataGrid>

How should i achieve it ?Please suggest.

Upvotes: 0

Views: 4253

Answers (2)

jimSampica
jimSampica

Reputation: 12410

Bind the checkbox to a property in your itemsource then you can get the selected row from the datagrid

<CheckBox HorizontalAlignment="Center" IsChecked="{Binding myBool}"/>  

MyDatagridItem item = MyDatagrid.SelectedItem as MyDatagridItem

if(item.myBool == true){
...
}

Upvotes: 1

tukaef
tukaef

Reputation: 9214

Use DataGridCheckBoxColumn instead:

<DataGridCheckBoxColumn Binding="{Binding IsChecked}" />

Upvotes: 1

Related Questions