更新数据表中的单元格 [英] Updating Cells in a DataTable

查看:118
本文介绍了更新数据表中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小应用程序,对CSV文件中的某些单元格进行一些处理。我已经弄清楚如何使用我在网上找到的库读取和写入CSV文件,但是我遇到了麻烦:图书馆将CSV文件解析成DataTable,但是当我尝试更改表格的单元格时, ,不保存表中的更改!

I'm writing a small app to do a little processing on some cells in a CSV file I have. I've figured out how to read and write CSV files with a library I found online, but I'm having trouble: the library parses CSV files into a DataTable, but, when I try to change a cell of the table, it isn't saving the change in the table!

以下是有问题的代码。我将流程分解成多个变量,并重命名了一些事情,以便更容易调试此问题。

Below is the code in question. I've separated the process into multiple variables and renamed some of the things to make it easier to debug for this question.

在循环中:

string debug1 = readIn.Rows[i].ItemArray[numColumnToCopyTo].ToString();
string debug2 = readIn.Rows[i].ItemArray[numColumnToCopyTo].ToString().Trim();
string debug3 = readIn.Rows[i].ItemArray[numColumnToCopyFrom].ToString().Trim();
string towrite = debug2 + ", " + debug3;
readIn.Rows[i].ItemArray[numColumnToCopyTo] = (object)towrite;

循环后:

readIn.AcceptChanges();






当我调试我的代码时, em> towrite 正在正确形成,一切都OK,除了行没有更新:为什么它不工作?我有一种感觉,我在这里犯了一个简单的错误:最后一次使用DataTables(很久以前),我有类似的问题。


When I debug my code, I see that towrite is being formed correctly and everything's OK, except that the row isn't updated: why isn't it working? I have a feeling that I'm making a simple mistake here: the last time I worked with DataTables (quite a long time ago), I had similar problems.

如果你想知道为什么我要在中添加另一个逗号,这是因为我将一个街道地址字段与一个邮政编码字段相结合 - 我希望这不会弄乱任何东西。

If you're wondering why I'm adding another comma in towrite, it's because I'm combining a street address field with a zip code field - I hope that's not messing anything up.

我的代码很混乱,因为我只是想编辑一个文件来做一个小修复,所以很抱歉。

My code is kind of messy, as I'm only trying to edit one file to make a small fix, so sorry.

推荐答案

编辑单个列值的最简单方法是使用 DataRow.Item indexer属性:

The easiest way to edit individual column values is to use the DataRow.Item indexer property:

readIn.Rows[i][numColumnToCopyTo] = (object)towrite;

这个没有很好的记录,但是 DataRow.ItemArray get 访问器返回底层数据的 复制 。以下是反射器的执行情况:

This isn't well-documented, but DataRow.ItemArray's get accessor returns a copy of the underlying data. Here's the implementation, courtesy of Reflector:

public object[] get_ItemArray() {
    int defaultRecord = this.GetDefaultRecord();
    object[] objArray = new object[this._columns.Count];
    for (int i = 0; i < objArray.Length; i++) {
        DataColumn column = this._columns[i];
        objArray[i] = column[defaultRecord];
    }
    return objArray;
}

编辑列值有一个尴尬的替代方法:获取一行的 ItemArray ,修改这些值,然后修改行以使用更新的数组:

There's an awkward alternative method for editing column values: get a row's ItemArray, modify those values, then modify the row to use the updated array:

object[] values = readIn.Rows[i].ItemArray;
values[numColumnToCopyTo] = (object)towrite;
readIn.Rows.ItemArray = values;

这篇关于更新数据表中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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