Ahmed Rabie
Ahmed Rabie

Reputation: 21

loop through checkboxcolumn in datagrid in wpf checked or not

i want to get the value of CheckBoxColumn in DataGrid wpf

i try this code

foreach (spShowTotal_Result item in dgShowStudent.ItemsSource)
        {

            bool? check = ((CheckBox)dgShowStudent.Columns[0].GetCellContent(item)).IsChecked;

        }

but this exception appear

Unable to cast object of type 'System.Windows.Controls.ContentPresenter' to type 'System.Windows.Controls.CheckBox'.

Upvotes: 0

Views: 2985

Answers (1)

Gopichandar
Gopichandar

Reputation: 2832

Seems like the workaround provided in comments not working out for you. Let me solve this in a different way.

Consider DataGrid as

<DataGrid x:Name="datagridexec">
    <DataGridTemplateColumn Header="DUT">
         <DataGridTemplateColumn.CellTemplate>                                                                   
               <DataTemplate>
                    <CheckBox x:Name="checkboxinstance"/>
               </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
</DataGrid>

and in your xaml.cs, you can access like below

  List<CheckBox> checkBoxlist = new List<CheckBox>();

  // Find all elements
  FindChildGroup<CheckBox>(datagridexec, "checkboxinstance", ref checkBoxlist );

  foreach (CheckBox c in checkBoxlist)
  {
      if (c.IsChecked)
      {
           //do whatever you want
      }      
  }

You need the following class to iterate through the tree.

    public static void FindChildGroup<T>(DependencyObject parent, string childName, ref List<T> list) where T : DependencyObject
    {
        // Checks should be made, but preferably one time before calling.
        // And here it is assumed that the programmer has taken into
        // account all of these conditions and checks are not needed.
        //if ((parent == null) || (childName == null) || (<Type T is not inheritable from FrameworkElement>))
        //{
        //    return;
        //}

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < childrenCount; i++)
        {
            // Get the child
            var child = VisualTreeHelper.GetChild(parent, i);

            // Compare on conformity the type
            T child_Test = child as T;

            // Not compare - go next
            if (child_Test == null)
            {
                // Go the deep
                FindChildGroup<T>(child, childName, ref list);
            }
            else
            {
                // If match, then check the name of the item
                FrameworkElement child_Element = child_Test as FrameworkElement;

                if (child_Element.Name == childName)
                {
                    // Found
                    list.Add(child_Test);
                }

                // We are looking for further, perhaps there are
                // children with the same name
                FindChildGroup<T>(child, childName, ref list);
            }
        }

        return;
    }

Reference: How to access datagrid template column textbox text WPF C#

Upvotes: 2

Related Questions