0

根据http://msdn.microsoft.com/en-us/library/ms810291.aspx,您不能更改 DataTable 中基于表达式的列的 ReadOnly 属性。我需要找出一种方法来评估 DataTable 列上的表达式,然后设置 .Expression = null,然后允许编辑该列:

以下代码不起作用,因为当您设置 Expression == null 时,它会将 DataTable 中的值重置为 DBNull:

_persistedDataTable.Columns[currentDataColumn.ColumnName].Expression = null;
_persistedDataTable.Columns[currentDataColumn.ColumnName].ReadOnly = false;

我的想法是,我将当前的 DataTable 逐行复制到一个新的 DataTable 中,而不保留架构,允许我编辑新的 DataTable 值(无论它们是否由表达式派生)......我只是不确定这该怎么做。

4

2 回答 2

0

我是这样做的:

String dateFilter = "IIF([data_type] = 'smalldatetime', 'true!', '')";
            String targetFilter = "[source_field]"; 

            DataTable tempDataTable = new DataTable();

            //create fields field
            DataColumn order = new DataColumn("order", Type.GetType("System.Int32"));
            tempDataTable.Columns.Add(order);
            order.AutoIncrement = true;
            order.AutoIncrementSeed = 1;
            order.ReadOnly = false;

            _sourceDataAdapter.Fill(tempDataTable);
            tempDataTable.Columns.Add(new DataColumn("date_format", Type.GetType("System.String"), dateFilter));
            tempDataTable.Columns.Add(new DataColumn("target_field", Type.GetType("System.String"), targetFilter));

            tempDataTable.BeginInit();
            tempDataTable.EndInit();

            _dataTable.Columns.Add(new DataColumn("order", Type.GetType("System.Int32")));
            _dataTable.Columns.Add(new DataColumn("local_field", Type.GetType("System.String")));
            _dataTable.Columns.Add(new DataColumn("length", Type.GetType("System.Int32")));
            _dataTable.Columns.Add(new DataColumn("data_type", Type.GetType("System.String")));
            _dataTable.Columns.Add(new DataColumn("decimals", Type.GetType("System.Int32")));

            _dataTable.Columns.Add(new DataColumn("date_format", Type.GetType("System.String")));
            _dataTable.Columns.Add(new DataColumn("target_field", Type.GetType("System.String")));

            using (DataTableReader reader = tempDataTable.CreateDataReader())
            {
                while(reader.Read())
                {
                    if (reader.HasRows)
                    {
                        _dataTable.Rows.Add(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), reader.GetValue(3), reader.GetValue(4), reader.GetValue(5), reader.GetValue(6));
                    }
                }
            }

        }
于 2012-06-27T16:00:53.320 回答
0

no you dont. You want to make a new column that has a copy of the expressioned data, and make that RW.

于 2019-01-29T19:57:14.093 回答