In WPF, how to clear DataBinding in a DataGrid?

10,384

Solution 1

The following line solved my problem ::

  BindingOperations.ClearAllBindings(dg);

Solution 2

To undo a binding in WPF, simply set the property that was previously bound to a some other value. In the case of DataGrid, its data is usually bound to the ItemsSource property so setting that to null will remove its previous binding. But if you have any other properties in the DataGrid that are bound, you will have to set those to "unbound" values as well. Which ones will depend on your situation. But in your example the code would be:

dg.ItemsSource = null;
Share:
10,384
Gurucharan Balakuntla Maheshku
Author by

Gurucharan Balakuntla Maheshku

“My objective is to provide solutions which can change the way people think and have a great impact on them” Software Engineering and Technology leader with 14+ years of experience in the IT industry leading and managing mid-scale to large scale digital systems (as Project Management, Product Management, People Management) across cross-functional teams and delivering highly performant, scalable, and secure systems. Well-versed in managing geographically distributed diverse teams across startups, MNC's and Enterprises. Deep knowledge in ​web architecture ​and ​technologies ​to solve challenging and complex business problems. Vast experience in recruiting and growing a team, instilling the right culture, motivating and retaining the best talents. I have served in diverse domains like Digital Web Apps, eCommerce, Payments, Enterprise applications, CRM, etc. with strong ​product and project management abilities.

Updated on June 04, 2022

Comments

  • Gurucharan Balakuntla Maheshku
    Gurucharan Balakuntla Maheshku almost 2 years

    I am using WPFToolKit DataGrid in my application. I have bound the DataGrid to a XMlDocument. The grid displays the Data from XML. I have to remove all the bindings in DataGrid and reset it during some event.

    Now my question is how do I remove the DataBinding between DataGrid and XMLDocument.

    I have tried something like this ::

    dg.SetValue(DataGrid.BindingGroupProperty, null); //doesn't work
    

    What am I doing wrong?