【发布时间】:2011-11-23 12:10:42
【问题描述】:
我在使用字符串数组中的项目填充 datagridview 时遇到问题。这是我用来调用函数的代码:
ThreadPool.QueueUserWorkItem((o) =>
ReBuildObjectExplorer();
还有函数本身:
try
{
List<ExplorerItem> list = new List<ExplorerItem>();
var item = new ExplorerItem();
for (int i = 0; i < lbl.Length; i++) // lbl = string array with items
{
item.title = lbl[i].Name;
list.Add(item);
}
BeginInvoke((MethodInvoker)delegate
{
explorerList = list;
dgvObjectExplorer.RowCount = explorerList.Count;
dgvObjectExplorer.Invalidate();
});
}
catch (Exception e) { MessageBox.Show(e.ToString(); }
问题是:假设数组中有 76 个项目。当我使用此代码时,它总是将第 75 项添加 76 次,仅此而已。为什么会这样?我似乎无法弄清楚我的代码有什么问题。
【问题讨论】:
标签: c# loops datagridview row populate