0
votes

So I created a ListView using Dataset, i.e. selecting ListView option for the table in the Data Sources Window and dragging it onto my interface.

Each of the columns have a Mode for binding (TwoWay/OneWay etc). They all got pre set to TwoWay i.e. here is a snippet of my ListView in the xaml file:

   <ListView x:Name="cbTableListView" Grid.ColumnSpan="2" ItemsSource="{Binding}" Margin="16,73,16,135" 
              SelectionChanged="onChangeRecord" SelectionMode="Single">
        <ListView.ItemContainerStyle>
            <Style>
                <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn x:Name="_idColumn" Header="ID" Width="60">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="10,-1,-6,-1" Text="{Binding _id, Mode=OneWay}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn x:Name="company_nameColumn" Header="Company Name" Width="120">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="-6,-1" Text="{Binding company_name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn x:Name="contact_nameColumn" Header="Contact Name" Width="120">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="-6,-1" Text="{Binding contact_name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

...and so on for the remaining columns

I need to have it so the user can edit the rows in the listview, press save changes, and have changes saved to the database. I'm assuming this is what the 'TwoWay' is all about? I've tried using the acceptchanges method on the dataset, doesnt seem to work:

        cBDataSet.AcceptChanges();
cBDataSetcbTableTableAdapter.Fill(cBDataSet.cbTable);
        cbTableViewSource = ((System.Windows.Data.CollectionViewSource)     (this.FindResource("cbTableViewSource")));
        cbTableViewSource.View.MoveCurrentToFirst();

Anyone any ideas? Not sure what i'm doing wrong

Thanks

1

1 Answers

0
votes

Via MSDN:

AcceptChanges Commits all the changes made to this DataSet since it was loaded or since the last time AcceptChanges was called.

This is very confusing. People usually only use the word "commit" when talking about databases, usually, but in this case MS screwed up on verbiage. AcceptChanges does nothing to the underlying data store, it simply tells that data set itself that the current changes are accepted, and to basically clear the dirty flag(s) and consider the current data in it as being up-to-date and matching the underlying store.

It does, however, go on to outline general usage of a DataSet:

Invoke the GetChanges method to create a second DataSet that features only the changes to the data.

Call the Update method of the DataAdapter, passing the second DataSet as an argument.

Invoke the Merge method to merge the changes from the second DataSet into the first.

Invoke the AcceptChanges on the DataSet. Alternatively, invoke RejectChanges to cancel the changes.

That should get you on the right track.