显示数据表中已删除的行 [英] Display deleted rows in a DataTable

查看:32
本文介绍了显示数据表中已删除的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 DataTable 绑定到 XAML 中的 DataGrid.允许用户添加、修改和删除表的行.我想用特定颜色标记行,具体取决于用户所做的操作.例如,如果用户添加一行,该行将被标记为绿色.如果用户修改一行,该行将被标记为橙色.如果用户删除该行,该行将被标记为红色.我遇到的问题是,一旦我从视图模型中调用 row.Delete(); ,删除的行就不再可见.

So I have a DataTable that I bind to the DataGrid in XAML. User is allowed to add, modify and remove rows for a table. I would like to mark rows with a specific colour, depending on the action that user makes. For instance, if user adds a row, that row will be marked as Green. If user modifies a row, that row will then be marked as orange. And if user removes the row, that row will be marked as red. The problem that I have is that the removed row is no longer visible once I call row.Delete(); from a view model.

有没有办法让 DataRow 标记为删除显示在 DataGrid 中?我知道如何根据用户操作实现行背景效果.唯一的问题是我不知道如何保持已删除的行可见.这个想法是用户可以恢复更改或应用它们,这就是实际删除待删除行的时间.

Is there a way to keep a DataRow marked for removal shown in a DataGrid? I know how to achieve the row background effect, depending on user action. The only problem is I don't know how to keep the deleted row visible. The idea is that user can either revert changes or apply them and that's when the pending deletion row should be actually deleted.

EDIT(添加了关于如何更新行背景颜色的示例):

EDIT (Added example on how I update row background colour):

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Path=Row.RowState}" Value="{x:Static data:DataRowState.Deleted}" />
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsSelected}" Value="False" />
    </MultiDataTrigger.Conditions>
    <MultiDataTrigger.Setters>
        <Setter Property="Background" Value="IndianRed" TargetName="DGR_Border"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontWeight" Value="Bold"/>
    </MultiDataTrigger.Setters>
</MultiDataTrigger>

推荐答案

我认为,当用户标记要删除的行时 - 您应该将它的索引保存在某处(int[] 数组或 List<int> 例如),然后在用户完成对表的处理后为该集合中的每个元素调用 yourDataGridView.Rows.RemoveAt(index).

I think, when user marks row for deleting - you should save it index somewhere (int[] array or List<int> for example), then call yourDataGridView.Rows.RemoveAt(index) for each element in that collection when user finished working with table.

也许是这样的:

//Collection for rows indexes which will be deleted later
List<int> rowsToDelete = new List<int>();

//Call this when user want to delete element
private void MarkElementsToRemove()
{
   if (yourDataGrid.SelectedItems.Count > 0)
   {
       //Get selected by user rows
       for (int i = 0; i < yourDataGrid.SelectedItems.Count; ++i)
       {
           DataGridRow row = (DataGridRow)yourDataGrid.SelectedItems[i];

           //Fill rows background with some color to mark them visually as "deleted"
           row.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));

           //Get row index to remove it later and add it to collection
           rowsToDelete.Add(row.GetIndex());                        
        }
    }
}

// Call this when user finished work with DataGrid and items may be removed
private void RemoveMarkedElements()
{
   foreach (int index in rowsToDelete)
   {
      yourDataGrid.Items.RemoveAt(index);
   }
   
   rowsToDelete.Clear();
}

您可以保存整个 DataGridRow 并调用 yourDataGrid.Remove(wholeRow);,而不是索引.对于反向删除,您只需通过删除颜色并从集合中删除行索引或整行来取消标记.

Instead of index you may save whole DataGridRow and call yourDataGrid.Remove(wholeRow);. And for reverse deletion, you just unmark it by removing color and removing row index or whole row from a collection.

这篇关于显示数据表中已删除的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆