none
How can I accept or cancel changes in a bound datagrid? RRS feed

  • Question

  • I have a DataGrid that is bound to a class.  It works well loading and saving data.  My problem is the data is saved as soon as someone press Enter.  I would like to have a Save button and Cancel button on the form that the user must press once they are happy/unsatisfied with their changes.  I seem to keep running into roadblocks.

    Here is the class I am using to bind to the DataGrid:

    public class RecipeDataProvider
        {
            private RicetteTableAdapter adapter;
            private RecipesDataSet dataset;
    
            public RecipeDataProvider()
            {
                dataset = new RecipesDataSet();
                adapter = new RicetteTableAdapter();
                adapter.Fill(dataset.Ricette);
    
                dataset.Ricette.RicetteRowChanged += new RecipesDataSet.RicetteRowChangeEventHandler(RecipesRowModified);
                dataset.Ricette.RicetteRowDeleted += new RecipesDataSet.RicetteRowChangeEventHandler(RecipesRowModified);
            }
    
            void Ricette_RicetteRowDeleted(object sender, RecipesDataSet.RicetteRowChangeEvent e)
            {
                throw new NotImplementedException();
            }
    
            void RecipesRowModified(object sender, RecipesDataSet.RicetteRowChangeEvent e)
            {
                try
                {
                    adapter.Update(dataset.Ricette);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            public void UpdateRecipes()
            {
            }
            public DataView GetRecipes()
            {
                return dataset.Ricette.DefaultView;
            }
        }

    Thanks for any help!

    Wednesday, August 22, 2012 5:08 PM

Answers

  • You only really show half the story, some xaml would help to answer exactly, so I'll just ramble and I hope it at least helps:

     

    Firstly, remove the "RowChanged - adapter.Update" bit.

    Then the rows will just persist in the DataGrid, until you add a button to save it:

    <Button Content="Save" Click="Button_Click"/>
     
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        adapter.Update();
    }

    As for reset, you don't show how you bind this data. I'd guess you could re-call the Fill in the delete button click handler:

    adapter.Fill(dataset.Ricette);
    

    Maybe you should implement INotifyPropertyChanged on your class, make dataset.ricette.DefaultView as a public property (like "MyDataView") and just call PropertyChanged("MyDataView"), to cause the DataGrid ItemsSorce to rebind.

    It's hard to answer exactly without seeing the full picture. Hope that helps.

     

    Regards,
    Pete


    #PEJL

    • Marked as answer by DChiShaggy Thursday, August 23, 2012 6:21 PM
    Thursday, August 23, 2012 12:40 AM
    Moderator

All replies

  • You only really show half the story, some xaml would help to answer exactly, so I'll just ramble and I hope it at least helps:

     

    Firstly, remove the "RowChanged - adapter.Update" bit.

    Then the rows will just persist in the DataGrid, until you add a button to save it:

    <Button Content="Save" Click="Button_Click"/>
     
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        adapter.Update();
    }

    As for reset, you don't show how you bind this data. I'd guess you could re-call the Fill in the delete button click handler:

    adapter.Fill(dataset.Ricette);
    

    Maybe you should implement INotifyPropertyChanged on your class, make dataset.ricette.DefaultView as a public property (like "MyDataView") and just call PropertyChanged("MyDataView"), to cause the DataGrid ItemsSorce to rebind.

    It's hard to answer exactly without seeing the full picture. Hope that helps.

     

    Regards,
    Pete


    #PEJL

    • Marked as answer by DChiShaggy Thursday, August 23, 2012 6:21 PM
    Thursday, August 23, 2012 12:40 AM
    Moderator
  • Yes I should have thrown in the code showing how I was binding the data. Here is that bit from the xaml:

    <Window.Resources> <ObjectDataProvider x:Key="RecipeDataProvider" ObjectType="{x:Type local:RecipeDataProvider}"/> <ObjectDataProvider x:Key="Ricette" ObjectInstance="{StaticResource RecipeDataProvider}" MethodName="GetRecipes"/> </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource Ricette}}........./>

    Doing it that way worked well for loading and updating the data, but I did not have any control of it from the code.  So I eliminated all of xaml I showed above.

    So now my code looks like this:

            public MainWindow()
            {
                InitializeComponent();
                dataGrid1.DataContext = rdp.GetRecipes();
            }

    And I can update with my button click:

             private void btnSave_Click(object sender, RoutedEventArgs e)
             {
                 rdp.UpdateRecipes();
             }

    Thursday, August 23, 2012 6:30 PM