如何防止重置DataRow单元格值?

时间:2022-09-21 22:22:35

We're creating a demonstration mode for our web application. We're going about this by creating new user that is tied to a real client, showing all of their data, but filtering certain fields to be non-identifiable (names, important numbers, etc...)

我们正在为我们的Web应用程序创建演示模式。我们通过创建绑定到真实客户端的新用户,显示他们的所有数据,但过滤某些字段是不可识别的(名称,重要数字等等)来实现这一目标。

The weird part here is that the data is correctly filtered for standard "show one" pages where I am calling the first function. When I try to call the second on a list page, the filter doesn't work. I have stepped through the functions, and it appears that in the first function, the values are being set, but are being reset back to the original values when the foreach iterates to the next row.

这里奇怪的部分是我正在调用第一个函数的标准“show one”页面正确过滤数据。当我尝试在列表页面上调用第二个时,过滤器不起作用。我已经逐步完成了这些函数,看起来在第一个函数中,值正在被设置,但是当foreach迭代到下一行时,它们被重置回原始值。

List mask is the list of column names that should be masked and is statically defined in the model.

列表掩码是应该屏蔽的列名列表,并在模型中静态定义。

public static void ApplyDemoMask(DataRow row, List<string> mask)
{
    foreach (DataColumn column in row.Table.Columns)
    {
        if (mask.Contains(column.ColumnName))
        {
            if (column.ColumnName == "ClientName")
                row[column] = "Demo Client";
            // More fields follow...
        }
    }
}

public static void ApplyDemoMask(FindResponse response, List<string> mask)
{
    foreach (DataRow row in response.ResultTable.Rows)
        ApplyDemoMask(row, mask);
}

I appreciate any help that can be given. I'm kind of dumb when it comes to System.Data for some reason.

我感谢任何帮助。出于某种原因,当谈到System.Data时,我有点愚蠢。

Edit: If it helps, when my debugger lands on the "}" in the second function, row["ClientName"] is "Demo Client" as it should be, but response.ResultTable.Rows[0]["ClientName"] is "The Original Client". Weird!

编辑:如果有帮助,当我的调试器落在第二个函数中的“}”时,行[“ClientName”]应该是“Demo Client”,但是response.ResultTable.Rows [0] [“ClientName”]是“原始客户”。奇怪的!

Edit: When binding to my grid, I have to specify column names. For one, we're using ASP.NET MVC and are using a custom control we wrote to help us transition from ASP.NET. Secondly, we have a lot of template fields. There can't be any huge, sweeping changes to this web application shares code with a pretty big WinForms line-of-business application.

编辑:绑定到我的网格时,我必须指定列名称。首先,我们使用ASP.NET MVC并使用我们编写的自定义控件来帮助我们从ASP.NET过渡。其次,我们有很多模板字段。使用相当大的WinForms业务线应用程序,这个Web应用程序共享代码不会有任何巨大的,彻底的变化。

3 个解决方案

#1


Try adding a call to

尝试添加呼叫

Row.AcceptChanges()

after the ForEach statement in ApplyDemoMask.

在ApplyDemoMask中的ForEach语句之后。

It seems your datagrid is binding to the current version of the rows, not the proposed version. Here are some references about rowstate for datarows in a datatable.

您的数据网格似乎绑定到当前版本的行,而不是建议的版本。以下是有关数据表中数据行的rowstate的一些参考。

#2


Maybe you deleted and added the same records many times. so you are updating the deleted one. change second method as :

也许你删除并多次添加相同的记录。所以你要更新删除的那个。将第二种方法更改为:

public static void ApplyDemoMask(FindResponse response, List<string> mask)
{
    foreach (DataRow row in response.ResultTable.Rows)
    {
        if (row.RowState != DataRowState.Deleted)
        {
            ApplyDemoMask(row, mask);
        }
    }
}

or like that :

或者像那样:

public static void ApplyDemoMask(FindResponse response, List<string> mask)
{
    response.ResultTable.AcceptChanges();
    foreach (DataRow row in response.ResultTable.Rows)
    {
        ApplyDemoMask(row, mask);
    }
}

hope this helps.

希望这可以帮助。

#3


I am an absent-minded imbecile!

我是一个心不在焉的笨蛋!

Check out ResultTable's definition...

查看ResultTable的定义......

public DataTable ResultTable { get { return this[_resultKey]; } }
public DataTable this[string tableName]
{
    get { return _data.Tables[tableName].DefaultView.ToTable(); }
}

I was making changes to the DataTable's DefaultView.ToTable(), which was being lost when going back to read ResultTable again. I completely forgot about this behavior and how our internal FindResponse class worked. It makes sense for our application, so I just added a similar BaseTable accessor that does not filter through DataTable.DefaultView, made my changes to that, and everything worked.

我正在更改DataTable的DefaultView.ToTable(),它在返回再次读取ResultTable时丢失了。我完全忘记了这种行为以及我们的内部FindResponse类是如何工作的。它对我们的应用程序有意义,所以我刚刚添加了一个类似的BaseTable访问器,它不会通过DataTable.DefaultView进行过滤,对我进行了更改,一切正常。

Thanks for the responses and sorry I didn't provide enough information.

感谢您的回复,抱歉,我没有提供足够的信息。

#1


Try adding a call to

尝试添加呼叫

Row.AcceptChanges()

after the ForEach statement in ApplyDemoMask.

在ApplyDemoMask中的ForEach语句之后。

It seems your datagrid is binding to the current version of the rows, not the proposed version. Here are some references about rowstate for datarows in a datatable.

您的数据网格似乎绑定到当前版本的行,而不是建议的版本。以下是有关数据表中数据行的rowstate的一些参考。

#2


Maybe you deleted and added the same records many times. so you are updating the deleted one. change second method as :

也许你删除并多次添加相同的记录。所以你要更新删除的那个。将第二种方法更改为:

public static void ApplyDemoMask(FindResponse response, List<string> mask)
{
    foreach (DataRow row in response.ResultTable.Rows)
    {
        if (row.RowState != DataRowState.Deleted)
        {
            ApplyDemoMask(row, mask);
        }
    }
}

or like that :

或者像那样:

public static void ApplyDemoMask(FindResponse response, List<string> mask)
{
    response.ResultTable.AcceptChanges();
    foreach (DataRow row in response.ResultTable.Rows)
    {
        ApplyDemoMask(row, mask);
    }
}

hope this helps.

希望这可以帮助。

#3


I am an absent-minded imbecile!

我是一个心不在焉的笨蛋!

Check out ResultTable's definition...

查看ResultTable的定义......

public DataTable ResultTable { get { return this[_resultKey]; } }
public DataTable this[string tableName]
{
    get { return _data.Tables[tableName].DefaultView.ToTable(); }
}

I was making changes to the DataTable's DefaultView.ToTable(), which was being lost when going back to read ResultTable again. I completely forgot about this behavior and how our internal FindResponse class worked. It makes sense for our application, so I just added a similar BaseTable accessor that does not filter through DataTable.DefaultView, made my changes to that, and everything worked.

我正在更改DataTable的DefaultView.ToTable(),它在返回再次读取ResultTable时丢失了。我完全忘记了这种行为以及我们的内部FindResponse类是如何工作的。它对我们的应用程序有意义,所以我刚刚添加了一个类似的BaseTable访问器,它不会通过DataTable.DefaultView进行过滤,对我进行了更改,一切正常。

Thanks for the responses and sorry I didn't provide enough information.

感谢您的回复,抱歉,我没有提供足够的信息。