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

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

问题描述

我正在写一个小的应用程序做一些细胞一点点处理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!

下面是code问题。我已经分离的过程分为多个变量,并改名为一些事情,使之更容易调试这个问题。

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


当我调试我的code,我看到的 towrite 的正在形成正确的,一切都OK,但该行不更新:为什么不工作?我有我做一个简单的错误在这里的感觉:我上次曾与数据表(相当长的时间以前),我有类似的问题。


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.

如果你想知道为什么我添加另一个逗号的 towrite 的,那是因为我结合一个街道地址字段,一个zip code场 - 我希望这不是瞎搞什么了起来。

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.

我的code是一种凌乱,因为我只是想编辑一个文件,使一个小补丁,很抱歉。

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

推荐答案

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

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

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

这是没有得到很好的记载,但 < $ C C> 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天全站免登陆