我可以使用for循环添加行数据吗? [英] Can I add row data with a for loop

查看:168
本文介绍了我可以使用for循环添加行数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码(for循环)向数据表添加行。调试器显示数据表行计数增量;但行列表从不填充。我是否在for循环中创建了某种范围问题?



I tried using the below code (for loop) to add rows to a datatable. The debugger shows the datatable row count increments; but the row list never populates. Am I creating some sort of scope issue with the for loop?

myTable.Columns.Add("Item1", typeof(int)); // set fixed number columns
myTable.Columns.Add("Item2", typeof(int));
myTable.Columns.Add("Item3", typeof(int));
                       
  for (int i = 0; i < numItems; i++)
  {
    DataRow myRow = myTable.NewRow();

    line = sr.ReadLine();
    subLine = line.Substring(line.LastIndexOf("= ") + 2); // find value

    myRow["Item1"] = int.Parse(subLine,System.Globalization.NumberStyles.Any);     

    myTable.Rows.Add( myRow );            // add row object to the table





我尝试了什么:



我尝试在for和inside内部声明NewRow对象环。外面我看到抛出一个异常。



What I have tried:

I've tried declaring the NewRow object inside and outside the for loop. Outside i see an exception thrown.

推荐答案

我不认为子串行是它。它似乎将int加载到行对象中而没有错误。只是为了确保我将其评论出来并替换为以下内容;

I don't think the Substring line is it. It seems to load the int into the row object without error. Just to be sure I commented it out and replaced with the following;
subLine = "123";

我得到相同的结果,行数仍然没有列表成员。



调试窗口列出了以下许多内容;

I get the same result, row counts and still no list members.

The debug widow list a lot of the following;

'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

我假设这些是状态消息??

I'm assuming these are status messages??


这篇关于我可以使用for循环添加行数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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