# VBForums CodeBank > CodeBank - C# >  WPF DataGrid Check/Uncheck All Functionality

## KGComputers

Here's a simple application I wrote before on how to add a checkbox with check/unchecked all functionality to a WPF DataGrid using DataGridTemplateColumnHeader.


To run the application, perform steps below.
1. ProductsSQL.sql
    - Change the target database of the script and execute. You may enter dummy records as well.
2. App.config
    - Update the connection string attribute with your database and server settings

Cheers!  :Smilie:

----------


## abcd_2014

KGComputers 

if uncheck 5 from 100 is there any way to loop throw checked only 

pessdo code

if is checked  then
update table
else
{
}

----------


## abcd_2014

this error appear

An unhandled exception of type 'System.InvalidCastException' occurred in WpfDatagridCheckAllHeader.exe

Additional information: Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.String]' to type 'System.Data.DataRowView'.

in this code 
DataRowView rv = (DataRowView)r.Item;



```
 private void SetCheckbox(IEnumerable<DataGridRow> row, bool value)
        {
            
            foreach (DataGridRow r in row)
            {

                DataRowView rv = (DataRowView)r.Item;
                foreach (DataGridColumn column in dgProducts.Columns)
                {
                    if (column.GetType().Equals(typeof(DataGridTemplateColumn)))
                    {
                        rv.Row["Discontinue"] = value;

                        //LINQ or Database Method to Update Product discontinue status
                        Connections.UpdateProductDiscontinue(value, rv.Row["productname"].ToString());
                    }
                }
            }
```

----------


## KGComputers

Try checking r.Item if it's not null.


C# Code:
if(r.item != null){    DataRowView rv = (DataRowView)r.Item;    foreach (DataGridColumn column in dgProducts.Columns)       {          //...       }}

----------

