【问题标题】:Datagrid save value in Database when cell change单元格更改时Datagrid将值保存在数据库中
【发布时间】:2019-12-03 13:31:47
【问题描述】:

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

<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"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

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

我的想法是制作一个ObservableCollection where I add the information of row changed (another ObservableCollectfor row deleted) and When the button Save is clicked, I save the value into Database taking the value intoObservableCollect`。你怎么看?

如何截取DataGrid单元格中值的变化?

private ObservableCollection<Book> books = null;
public ObservableCollection<Book> Books
{
    get { return books; }
    set
    {
        books = value;
        NotifyPropertyChanged();
    }
}

如果我更改单元格值,这点不是调用?

感谢您的帮助。

【问题讨论】:

    标签: c# wpf mvvm datagrid


    【解决方案1】:

    有几种方法可以处理此问题,但我不一定建议“拦截”更改以跟踪它们(尤其是如果您使用“保存”按钮)。相反,当您从数据库加载Books 列表时,我会制作两个副本:BooksBooksOriginal(或类似名称)。 Books 将是 ObservableCollection&lt;T&gt;,但 BooksOriginal 可以只是普通的 List&lt;T&gt;,因为它不会用于绑定。

    DataGrid 绑定到Books 并让用户添加、删除或更新他们想要的任何记录。然后,当他们单击“保存”时,您将 BooksBooksOriginal 进行比较以决定如何处理每个项目:

    • 如果来自Books 的项目不在BooksOriginal 中,则它是新的,需要插入到数据库中。
    • 如果来自Books 的项目在BooksOriginal 中,请比较两个版本以查看它们是否不同。如果是这样,则进行了更改,您需要更新数据库中的记录。
    • 最后,检查BooksOriginal 中没有Books 的项目,那些将是已从DataGrid 中删除的记录。

    这样数据绑定就可以完成它的工作,您只需要担心两个变量(BooksBooksOriginal),所有逻辑/代码都保存在一个方法中:Save

    几个注意事项:
    这假定每条记录都有一个唯一的标识符。

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

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

    【讨论】:

    • 非常感谢我会遵循的建议。
    猜你喜欢
    • 2013-05-01
    • 2011-07-29
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    相关资源
    最近更新 更多