views:

6441

answers:

6

I need some c# code to select / deselect all checkboxes in a datagrid in WPF 3.5 framework. I would like to do this by clicking a single header checkbox in the grid.

Please help.

A: 

This is based on someone else's source that I can't recall, but we use it to help find visual children of a type. It may not be the most efficient use for this scenario but it might help get you on the right track.

    public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
 {
  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  {
   DependencyObject child = VisualTreeHelper.GetChild(obj, i);
   if (child != null && child is childItem)
    return (childItem)child;

   childItem childOfChild = FindVisualChild<childItem>(child);
   if (childOfChild != null)
    return childOfChild;
  }
  return null;
 }

[Edit 4.16.09] Based on that, try out this method. Should find all CheckBoxes and change the state as provided, callable from your event handler on the Checked/Unchecked events.

   public static void CheckAllBoxes(DependencyObject obj, bool isChecked)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            // If a checkbox, change IsChecked and continue.
            if (obj is CheckBox)
            {
                ((CheckBox) obj).IsChecked = isChecked;
                continue;
            }

            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            CheckAllBoxes(child, isChecked);
        }
    }
Jeff Wain
A: 

I would use the new databinding features in WPF. Give all of the checkboxes a one-way binding on their IsChecked property with the binding source being the master checkbox's IsChecked property.

Another option would be to use triggers

Jacob Adams
A: 

Hi,

I want code for select all checkbox in datagrid in C# winforms.

Hope you can help me..

Regards, Shyam

-1 For attempting to hijack this Question. Shyam, StackOverflow is not exactly like a typical forum where contributors change the subject often (hijacking). Here we typically expect strictly question and answers with occasional side topics as comments on either of these if they apply in some way to the content.
jpierson
+1  A: 

This can be done declaratively. The following creates a checkbox column for each row and which can toggle row selections. The header of the checkbox column can be clicked to do a select all of the rows.

Relevant portions from the xaml

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"&gt;
    <toolkit:DataGrid Name="dataGrid" 
     ItemsSource="{Binding}" AutoGenerateColumns="True" 
     SelectionMode="Extended" CanResizeRows="False">
    <toolkit:DataGrid.RowHeaderTemplate>
       <DataTemplate>
           <Grid>
               <CheckBox IsChecked="{
                  Binding Path=IsSelected, 
                  Mode=TwoWay, 
                  RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type toolkit:DataGridRow}}}"
            />
           </Grid>
       </DataTemplate>
    </toolkit:DataGrid.RowHeaderTemplate>
    </toolkit:DataGrid>
</Window>
Pradeep
A: 
DataColumn dt = null;            
        for (int i = 0; i < dataGrid.Columns.Count; i++)
        {

     dt = new DataColumn("Column Name");

            dt.DataType = typeof(Boolean);
            dt.DefaultValue = false;
             dataTable.Add(dt);
        }

dataGied.DataContext = dataTable;



<Custom:DataGrid x:Name="dataGrid" AutoGenerateColumns="True" ItemsSource="{Binding}" />

Well it is just a rough Idea, hope this works

Aizaz
A: 

Hi, I have the checkbox in the RowHeader. How can I get the checkbox isChecked value by loop through the records in the datagrid?

CYK