簡體   English   中英

單元格更改時,Datagrid 在數據庫中保存值

[英]Datagrid save value in Database when cell change

嗨,我創建了這個測試代碼:

<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>

我創建了兩個按鈕和一個DataGrid DataGrid填充讀入數據庫的值,現在如果用戶將文本更改為單元格,則新值將保存到數據庫中(暫時沒有實現按鈕X 刪除到行中)。

我的想法是創建一個ObservableCollection where I add the information of row changed (another ObservableCollect for row deleted) and When the button Save is clicked, I save the value into Database taking the value into ObservableCollect`。 你怎么認為?

如何攔截DataGrid單元格中值的變化?

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

如果我更改單元格值,這點不是調用嗎?

感謝您的幫助。

有幾種方法可以解決這個問題,但我不一定建議“攔截”更改以跟蹤它們(特別是如果您使用“保存”按鈕時)。 相反,當您從數據庫加載Books列表時,我會制作兩個副本: BooksBooksOriginal (或一些類似的名稱)。 Books將是一個ObservableCollection<T> ,但BooksOriginal可以只是一個普通的List<T>因為它不會用於綁定。

DataGrid綁定到Books並讓用戶添加、刪除或更新他們想要的任何記錄。 然后,當他們單擊“保存”時,您將BooksBooksOriginal進行比較以決定如何處理每個項目:

  • 如果Books的項目不在BooksOriginal ,則它是新的,需要插入到數據庫中。
  • 如果Books的項目在BooksOriginal ,請比較兩個版本以查看它們是否不同。 如果是,則進行了更改,您需要更新數據庫中的記錄。
  • 最后,檢查BooksOriginal中不在Books ,那些將是已從DataGrid刪除的記錄。

這樣數據綁定就可以完成它的工作,您只需要擔心兩個變量( BooksBooksOriginal ),並且所有邏輯/代碼都保存在一個方法中: Save

一些注意事項:
這假設每個記錄都有一個唯一的標識符。

請確保,如果您使用上述兩個集合的想法,則為每本“書”制作兩個單獨的對象 不要制作一本書對象並將其添加到兩個集合中,否則您將無法比較更改。

您還可以選擇在程序加載時僅創建一個列表 ( Books ),然后在單擊保存時加載第二個列表 ( BooksOriginal )。 這樣做的好處是確保您在檢查更改時擁有數據庫中的最新數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM