对DataRow的RowState更改做出反应

时间:2019-05-06 10:53:02

标签: c# events dataset

为了获得更好的用户信息,我在DataGrid中添加了一列向用户显示当前行的RowState。为此,我编写了一个事件处理程序,侦听基础DataTable的“更改”事件。它运作良好,显示未修改和正确修改的新行。我只有一个问题。

问题

DataRowDataTableDataSet的一部分,是从数据库中查询的数据。因此,rowstate列工作正常,直到我在数据集上调用save为止。然后,RowState更改为“ NotModified”,但是我的符号没有更改。
在保存(或更改也拒绝)后,我可以使用哪些事件或方法来对RowState更改做出反应?

我真的不能加入“保存”方法,因为我只想对表和行进行通用操作。

那么有解决方案吗,还是我必须自定义所有保存方法?

代码

有关更多信息,我将显示用于激活 DataGrid的RowState列的代码。

    /// <summary>
    ///     Adds a <see cref="DxIconColumn" /> to the <see cref="DxDataGrid" /> which displays the rowstate of the rows. Icons for modified, new and unchanged are added.
    ///     <para />
    ///     This method also works if the column is already defined manually and makes changed accordingly.
    ///     <para />
    ///     <b>Attention!</b> This method only works if the Datasource <see cref="DataTable" /> contains a columns with the specified name in <paramref name="rowStateColumnName" />.
    ///     This column is filled automatically with the rowstate.
    /// </summary>
    /// <param name="datagrid">The <see cref="DxDataGrid" /> to activate the column on</param>
    /// <param name="rowStateColumnName">Optional a different name of the rowstate column in the <see cref="DataTable" /></param>
    public static void ActivateOnGrid([NotNull] DxDataGrid datagrid, [NotNull] string rowStateColumnName = "cf_rowstate")
    {
        Guard.Parameter.NotNull(datagrid, "datagrid", "datagrid != null");
        Guard.Parameter.NotNull(rowStateColumnName, "rowStateColumnName", "rowStateColumnName != null");

        // first check that the DataSource contains the binding column we want to use for the rowstates. Otherwise we sadly have to cancel
        if (!datagrid.TableSource.HasColumn(rowStateColumnName))
        {
            LoggerSink.PushMessage(null, string.Format("Could not activate rowstate column for grid '{0}' because column '{1}' does not exist in the binded DataTable.", datagrid.Name, rowStateColumnName), Severity.Warning);
            return;
        }

        // First verify that we modify an existing column if it already exists
        var column = datagrid.Columns.OfType<DxIconColumn>()
            .FirstOrDefault(x => x.BindingPath == rowStateColumnName || x.Name.StartsWith("tirowstate", StringComparison.Ordinal));
        if (column == null)
        {
            column = new DxIconColumn
            {
                Name = string.Format("tirowstate_{0}", datagrid.Name),
                BindingPath = rowStateColumnName,
                Header = "",
                Width = 20,
                ImageHeight = 15,
                ImageWidth = 15,
                IsReadOnly = true,
                Visibility = Visibility.Visible,
                VisibleIndex = 0
            };
            datagrid.Columns.Add(column);
        }

        // Verify that the different icons exist.
        var neededIcons = new[]
        {
            new { RowState = EnumRowState.NotModified, Icon = clientGlobals.Icons.LedGray },
            new { RowState = EnumRowState.EditingNew, Icon = clientGlobals.Icons.New },
            new { RowState = EnumRowState.Modified, Icon = clientGlobals.Icons.LedYellow }
        };

        // Add each missing icon
        foreach (var iconData in neededIcons)
        {
            if (column.Icons.Any(x => x.Condition.Contains(string.Format(@"""{0}""", iconData.RowState))))
                continue;

            var icon = new DxIcon
            {
                Condition = string.Format(@"@{0} == ""{1}""", rowStateColumnName, iconData.RowState),
                ImageName = iconData.Icon.ToString()
            };
            column.Icons.Add(icon);
        }

        // Now subscribe the event methods to update the rowstate column accordingly
        datagrid.DGItemChanged += (o, args) => SetRowStateOnSelectedRow(datagrid, rowStateColumnName);
        datagrid.CellValueChanged += (o, args) => SetRowStateOnSelectedRow(datagrid, rowStateColumnName);
        datagrid.New += (sender, args) => SetRowStateOnSelectedRow(datagrid, rowStateColumnName);
        datagrid.Loaded += (sender, args) => SetRowState(datagrid, rowStateColumnName);
    }

0 个答案:

没有答案