遍历ListBox中的所有项目? [英] Looping through all items in ListBox?

查看:88
本文介绍了遍历ListBox中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由以下代码填充的列表框:

I have a list box which is populated by this code:

  • lstOutcome.Items.Add(lstMap.SelectedItem.Text);

在列表框lstOutcome中,我需要能够遍历列表框并能够采用第一,第二等列表项.

In the listbox lstOutcome, I need to be able to loop through the listbox and be able to take the value of the first,second, etc, list items.

我需要遍历每行并抓住该行的值是,所以我可以使用其中的任何值该行用于填充其他内容.

The reason I need to loop through each line and grab the value of that line is so I can use whatever value was in that line for populating something else.

例如,在我的列表框中,我有:

For example, in my list box I have:

  • 1
  • 2
  • 3

我需要能够遍历按钮上的列表框点击,并将值分配给txtboxes:

I need to be able to loop through the listbox on button click and have the values assigned to txtboxes:

  • textbox1.Text ='列表框中的项目1';
  • textbox2.Text ='列表框中的项目2';
  • textbox3.Text ='列表框中的项目3';

我不确定我是否需要一个数组或该如何做完成.这个例子的重点是我将实际上是使用列表框中的项目来映射列.一世正在导入Microsoft Excel电子表格.在lstMap中,我有列名称,我正在尝试使列名称匹配我的数据库.使用此代码,我试图获取值列表框:

I am not sure if I need an array or how this can be accomplished. The point of this example is that I will actually be using the items in the listbox to map columns. I am importing an Microsoft Excel spreadsheet. In lstMap I have the column names and I am trying to get the column names to match my database. Using this code I am trying to take the values of the listbox:

foreach(object li in lstOutcome.Items)
{
    bulkCopy.DestinationTableName = "Customers";
    //Amount to bulkcopy at once to prevent slowing with large imports.
    bulkCopy.BatchSize = 200;
    SqlBulkCopyColumnMapping map1 = new SqlBulkCopyColumnMapping(li.ToString(), "CustomerID");
    bulkCopy.ColumnMappings.Add(map1);

推荐答案

写答案的更有效方法如下:

A more efficient way to write your answer would be like this:

static readonly string[] FieldNames = new string[] { "CustomerID", "Name", "Address", ..., "Email" };

using(SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString, options)) {

    bulkCopy.DestinationTableName = "Customers";
    //amount to bulkcopy at once to prevent slowing with large imports
    bulkCopy.BatchSize = 200;

    for(int i = 0; i < FieldNames.Length; i++) {
        bulkCopy.ColumnMappings.Add(
            new SqlBulkCopyColumnMapping(lstOutcome.Items[i].ToString(), FieldNames[i])
        );
    }
}

这篇关于遍历ListBox中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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