更新数据表中的单元格

发布于 2024-08-25 09:01:52 字数 913 浏览 5 评论 0原文

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

下面是有问题的代码。我已将过程分成多个变量,并重命名了一些内容,以便更轻松地调试这个问题。

代码

循环内的

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();

当我调试代码时,我看到 towrite 已正确形成,并且一切正常,只是该行未更新:为什么它不工作?我有一种感觉,我在这里犯了一个简单的错误:上次我使用 DataTables 时(很久以前),我遇到了类似的问题。

如果您想知道为什么我在 towrite 中添加另一个逗号,那是因为我将街道地址字段与邮政编码字段组合在一起 - 我希望这不会弄乱任何事情。

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

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.

Code

Inside the loop:

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;

After the loop:

readIn.AcceptChanges();

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-09-01 09:01:52

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

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

这没有详细记录,但是 DataRow.ItemArrayget 访问器返回副本的基础数据。这是实现,由 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,修改这些值,然后修改该行以使用更新后的数组:

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

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

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

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;
}

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;
若相惜即相离 2024-09-01 09:01:52

使用 SetField<> 方法:

    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].SetField<string>(numColumnToCopyTo,towrite); 

    readIn.AcceptChanges();

use SetField<> method :

    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].SetField<string>(numColumnToCopyTo,towrite); 

    readIn.AcceptChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文