麻烦在C#中更新的DataRow [英] Trouble with updating DataRow in C#

查看:201
本文介绍了麻烦在C#中更新的DataRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我似乎无法绕到我的头一个非常简单的C#的DataTable的问题;它似乎很两岸前锋,但我一定是失去了一些东西。

I have a very simple C# DataTable problem that I cannot seem to wrap my head around; it seems so strait forward but I must be missing something.

我希望有人可以给我,为什么我无法更新显示在一个DataTable像一个单元格的值解释如下:

I was hoping that someone could explain to me why I'm unable update a cell value in a DataTable like shown below:

代码:

    DataTable t = new DataTable();
    t.Columns.Add("MyCol");
    t.Rows.Add("old value");
    t.Rows[0].ItemArray[0] = "new value";
    t.AcceptChanges();
    dataGridView1.DataSource = t;         //did not work. still reads "old value"



任何帮助,将不胜感激! !谢谢

Any help would be appreciated! thanks!

推荐答案

简单改变:

t.Rows[0].ItemArray[0] = "new value";



to

t.Rows[0][0] = "new value";

这就是它!

编辑(新增交代):

更改ItemArray元素是不被跟踪,所以没有更改反映在数据表中的值(在原来的问题代码)

Changes to ItemArray elements are not tracked, so no changes are reflected in the datatable values (code in the original question)

不过,您可以使用ItemArray来一次更改所有的行,像这样的:

However you can use ItemArray to change all the row at once, like this:

t.Rows[0].ItemArray = new object[] {"new value"};

在这种情况下,变化跟踪,你会得到预期的结果。

In this case the changes are tracked, and you get the expected result.

这篇关于麻烦在C#中更新的DataRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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