Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单元格更改时Datagrid在数据库中保存值_C#_Wpf_Mvvm_Datagrid - Fatal编程技术网

C# 单元格更改时Datagrid在数据库中保存值

C# 单元格更改时Datagrid在数据库中保存值,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,嗨,我创建了这个测试代码: <Button x:Name="b_Delete" Content="Delete" Margin="10" Grid.Row="1"/> <Button x:Name="b_Save" Content="Save" Margin="10" Grid.Row="1" Grid.Column="1" Command="{Binding Path=SaveCommand}"/> <DataGrid x:Name="dataG

嗨,我创建了这个测试代码:

 <Button x:Name="b_Delete" Content="Delete" Margin="10" Grid.Row="1"/>
    <Button x:Name="b_Save" Content="Save" Margin="10" Grid.Row="1" Grid.Column="1" Command="{Binding Path=SaveCommand}"/>
    <DataGrid x:Name="dataGrid" Margin="10" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Books, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Author" Header="Author"  Width="*" Binding="{Binding author, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <DataGridTextColumn x:Name="Title" Header="Title"   Width="*" Binding="{Binding title}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Left"  />
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <DataGridTextColumn x:Name="Price" Header="Price" Width="*" Binding="{Binding price}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Left" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>


            <DataGridTemplateColumn x:Name="Delete_Row" Header="Delete" Width="0.2*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

我创建了两个按钮和一个数据网格。要使用读入数据库的值填充datagrid,现在如果用户将文本更改为单元格,则新值将保存到数据库中(暂时不实现删除到行中的按钮X)

我的想法是创建一个ObservableCollection,在其中添加更改行的信息(另一个ObservableCollect用于删除行) 当点击Save按钮时,我将值保存到数据库中,并将值保存到ObservableCollect中。你觉得怎么样

如何在datagrid的单元格中拦截值的更改

 private ObservableCollection<Book> books = null;
    public ObservableCollection<Book> Books
    {
        get { return books; }
        set
        {
            books = value;
            NotifyPropertyChanged();
        }
    }
private observeCollection books=null;
公开收集书籍
{
获取{归还图书;}
设置
{
书籍=价值;
NotifyPropertyChanged();
}
}
如果我更改单元格值,这一点不被调用


感谢您的帮助

有几种方法可以解决这个问题,但我不建议您“拦截”更改以跟踪它们(特别是如果您使用“保存”按钮)。相反,当您从数据库加载
图书
列表时,我会制作两个副本:
图书
图书原件
(或类似名称)
Books
将是一个
ObservableCollection
,但
BooksOriginal
可以只是一个普通的
列表,因为它不会用于装订

数据网格
绑定到
书籍
,让用户添加、删除或更新他们想要的任何记录。然后,当他们单击“保存”时,您将
图书
图书原件
进行比较,以决定如何处理每个项目:

  • 如果
    图书
    中的项目不在
    图书原始
    中,则该项目是新的,需要插入到数据库中
  • 如果
    图书
    中的项目位于
    图书原件
    中,请比较这两个版本是否不同。如果是这样,则进行了更改,您需要更新数据库中的记录
  • 最后,检查
    BooksOriginal
    中不在
    Books
    中的项目,这些将是从
    数据网格中删除的记录
这样,数据绑定就可以完成它的工作,您只需要担心两个变量(
Books
BooksOriginal
),所有逻辑/代码都保存在一个方法中:
Save

两个注释:
这假设每条记录都有一个唯一的标识符

如果您使用上述两个集合的想法,请确保为每个“书”创建两个单独的对象。不要创建一个book对象并将其添加到两个集合中,否则您将无法比较更改


您还可以选择在程序加载时仅制作一个列表(
Books
),然后在单击保存时加载第二个列表(
BooksOriginal
)。这样做的好处是,在检查更改时确保您拥有数据库中最新的数据。

非常感谢我将遵循的建议。