将值添加到特定的DataTable单元格 [英] Adding values to specific DataTable cells
问题描述
我想知道是否可以向特定的DataTable单元格添加值吗?
I'm wondering if it's possible to add values to specific DataTable cells?
假设我有一个现有的dataTable并添加了一个新列,我将如何处理关于添加新列的行而不覆盖现有列的行?
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)
其中a,b,c和d是字符串值。那如果我只想添加到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屋!