I slightly modified Your XAML:
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Grid.RowSpan="2" Height="130" HorizontalAlignment="Left" IsReadOnly="False" Margin="189,340,0,0" Name="dgCandidate" TabIndex="7" VerticalAlignment="Top" Width="466" Grid.Row="1" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="colCandidateID" Binding="{Binding CandidateID}" Header="SlNo" MinWidth="20" IsReadOnly="True" />
<DataGridTextColumn x:Name="colRegistraion" Binding="{Binding RegisterNo}" Header="Reg. No." IsReadOnly="True" />
<DataGridTextColumn x:Name="colCandidate" Binding="{Binding CandidateName}" Header="Name" MinWidth="250" IsReadOnly="True" />
<DataGridCheckBoxColumn Binding="{Binding IsSelected}">
<DataGridCheckBoxColumn.Header>
<CheckBox Name="chkSelectAll" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"></CheckBox>
</DataGridCheckBoxColumn.Header>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
And wrote a Candidate class like this:
public class Candidate : INotifyPropertyChanged
{
public int CandidateID { get; set; }
public int RegisterNo { get; set; }
public string CandidateName { get; set; }
private bool _IsSelected = false;
public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnChanged("IsSelected"); } }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnChanged(string prop)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
#endregion
}
Note the IsSelected property which stores if a Candidate is selected or not.
And finally this is the interaction logic of the check all checkbox:
private void chkSelectAll_Checked(object sender, RoutedEventArgs e)
{
foreach (Candidate c in dgCandidate.ItemsSource)
{
c.IsSelected = true;
}
}
private void chkSelectAll_Unchecked(object sender, RoutedEventArgs e)
{
foreach (Candidate c in dgCandidate.ItemsSource)
{
c.IsSelected = false;
}
}
Finally I initialize the datagrid items source like this:
List<candidate> tmp = new List<candidate>();
tmp.Add(new Candidate()
{
CandidateID = 1,
CandidateName = "One",
RegisterNo = 1
});
tmp.Add(new Candidate()
{
CandidateID = 2,
CandidateName = "Two",
RegisterNo = 2
});
tmp.Add(new Candidate()
{
CandidateID = 3,
CandidateName = "Three",
RegisterNo = 3
});
dgCandidate.ItemsSource = tmp;
The view gets refreshed automatically when the IsSelected changes.