Click here to Skip to main content
14,636,132 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have DataGrid with one CheckBoxColumn in header of that CheckBox column I have added CheckBox to Select all CheckBox in Datagrid Row..


So I tell me the code for that...

My XAML Code for WPF dataGrid

<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"  />

        <DataGridTemplateColumn>
            <DataGridTemplateColumn.Header>
                <CheckBox Name="chkSelectAll" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"></CheckBox>
            </DataGridTemplateColumn.Header>
            <DataGridTemplateColumn.CellTemplate >
                <DataTemplate >
                    <CheckBox x:Name="colchkSelect1" Checked="colchkSelect1_Checked" Unchecked="colchkSelect1_Unchecked" ></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
v2
Comments
Luckyboykg 28-Sep-13 4:13am
   
When I checked in checkbox is show message error : "edit item is not allowed for this view."

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