通过 for 循环从 List 更新数据行。索引越界
Updating Datarow from List via for loop. Index was out of bounds
我有两个字符串列表:
currentRow = 包含该行应具有的信息
currentCol = 包含来自 currentRow 的数据应该进入的列的名称。
每个 List 包含 25(0-24) 个项目,其排序方式与其应写入的 dataRow 相同。
我在此处填写列表,使用表单上的标签和文本框:
List<string> currentRow = new List<string>();
List<string> currentCol = new List<string>();
foreach (var c in form11.Controls)
{
if (c.GetType() == typeof(TextBox))
{
var str = c.ToString();
var str1 = str.Substring(35);
currentRow.Add(str1);
}
if (c.GetType() == typeof(Label))
{
var str = c.ToString();
var str1 = str.Substring(34);
currentCol.Add(str1);
}
}
然后我 select dataTable 中需要从 currentRow 中的第 3 项更新的行,这是一个唯一标识符。
var updateRow = arraysDt.Select("SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'");
现在我尝试更新列表中项目的行:
for (int i = 0; i < currentRow.Count; i++)
{
//MessageBox.Show(currentCol.ElementAtOrDefault(i).ToString() + " " + currentRow.ElementAtOrDefault(i).ToString());
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
}
一旦它进入 for 循环,我就会抛出一个 "index was out of bounds of the array" 错误。
正如我所说,currentCol 包含列名,currentRow 是值。
所以当它到达这里时,我希望它找到列名,然后用值更新它。
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
我做错了什么?
我发现了问题:
"SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'"
会给我这个:
序列号='XXXXX'
我需要的是:
序列号='XXXXX'
所以为了修复它我做了:
string SMnum = currentRow.ElementAt(2).ToString().Replace(" ", string.Empty);
string query = string.Format("SERIAL='{0}'", SMnum.Replace(@"'", "''"));
var updateRow = arraysDt.Select(query);
这会删除我要查找的字符串中的任何白色 space。
我有两个字符串列表:
currentRow = 包含该行应具有的信息
currentCol = 包含来自 currentRow 的数据应该进入的列的名称。
每个 List 包含 25(0-24) 个项目,其排序方式与其应写入的 dataRow 相同。
我在此处填写列表,使用表单上的标签和文本框:
List<string> currentRow = new List<string>();
List<string> currentCol = new List<string>();
foreach (var c in form11.Controls)
{
if (c.GetType() == typeof(TextBox))
{
var str = c.ToString();
var str1 = str.Substring(35);
currentRow.Add(str1);
}
if (c.GetType() == typeof(Label))
{
var str = c.ToString();
var str1 = str.Substring(34);
currentCol.Add(str1);
}
}
然后我 select dataTable 中需要从 currentRow 中的第 3 项更新的行,这是一个唯一标识符。
var updateRow = arraysDt.Select("SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'");
现在我尝试更新列表中项目的行:
for (int i = 0; i < currentRow.Count; i++)
{
//MessageBox.Show(currentCol.ElementAtOrDefault(i).ToString() + " " + currentRow.ElementAtOrDefault(i).ToString());
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
}
一旦它进入 for 循环,我就会抛出一个 "index was out of bounds of the array" 错误。
正如我所说,currentCol 包含列名,currentRow 是值。 所以当它到达这里时,我希望它找到列名,然后用值更新它。
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
我做错了什么?
我发现了问题:
"SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'"
会给我这个:
序列号='XXXXX'
我需要的是:
序列号='XXXXX'
所以为了修复它我做了:
string SMnum = currentRow.ElementAt(2).ToString().Replace(" ", string.Empty);
string query = string.Format("SERIAL='{0}'", SMnum.Replace(@"'", "''"));
var updateRow = arraysDt.Select(query);
这会删除我要查找的字符串中的任何白色 space。