将值添加到特定的 DataTable 单元格 [英] Adding values to specific DataTable cells
问题描述
我想知道是否可以向特定的 DataTable 单元格添加值?
假设我有一个现有的 dataTable 并且我添加了一个新列,我将如何在不覆盖现有列的行的情况下添加到新列的行?
据我所知,没有添加到特定单元格的方法(除非我错了).
dt.Rows.Add(a, b, c, d)
其中 a、b、c 和 d 是字符串值.那么如果我只想添加到 d 列呢?
任何帮助将不胜感激.
如果是一个全新的行,您只想设置一个值,则需要添加整行,然后设置单个值:
>
DataRow dr = dt.NewRow();dr[3].Value = "一些价值";dt.Rows.Add(dr);
否则,您可以找到现有行并设置单元格值
DataRow dr = dt.Rows[theRowNumber];dr[3] = "新值";
I'm wondering if it's possible to add values to specific DataTable cells?
Suppose I have an existing dataTable and I add a new column, how would I go about adding to the new column's rows without overwriting the existing columns' rows?
As far as I'm aware, there isn't a method for adding to specific cells (unless I'm wrong).
dt.Rows.Add(a, b, c, d)
where a, b, c and d are string values. So what if I just want to add to the d column?
Any help would be appreciated.
If it were a completely new row that you wanted to only set one value, you would need to add the whole row and then set the individual value:
DataRow dr = dt.NewRow();
dr[3].Value = "Some Value";
dt.Rows.Add(dr);
Otherwise, you can find the existing row and set the cell value
DataRow dr = dt.Rows[theRowNumber];
dr[3] = "New Value";
这篇关于将值添加到特定的 DataTable 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!