C# 循环遍历 ListBox 中的所有项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:35:43  来源:igfitidea点击:

Looping through all items in ListBox?

c#.netasp.netarrayslistboxitem

提问by

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

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

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

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.

在列表框 lstOutcome 中,我需要能够遍历列表框并能够获取第一个、第二个等列表项的值。

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
  • 1
  • 2
  • 3

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

我需要能够在单击按钮时遍历列表框并将值分配给 txtboxes:

  • textbox1.Text = 'item 1 in the listbox';
  • textbox2.Text = 'item 2 in the listbox';
  • textbox3.Text = 'item 3 in the listbox';
  • textbox1.Text = '列表框中的第 1 项';
  • textbox2.Text = '列表框中的第 2 项';
  • textbox3.Text = '列表框中的第 3 项';

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:

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

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);

回答by SLaks

You need to put the textboxes in an array, like this:

您需要将文本框放在一个数组中,如下所示:

Textbox[] textboxes = new Textbox[] { textbox, textbox2, textbox3 };

for (int i = 0; i < listBox1.Items.Count; i++) {
    textboxes[i].Text = "Item: " + listBox1.Items[i].ToString();
}

Note that if there are more than three items in the listbox, this will fail because it will run out of textboxes. For a more complete solution, please add some context. What are the textboxes for? What is the data in the listbox coming from?

请注意,如果列表框中的项目超过三个,这将失败,因为它将用完文本框。如需更完整的解决方案,请添加一些上下文。文本框有什么用?列表框中的数据来自什么?

回答by SLaks

I got it to work, check out this code amigo's

我让它工作了,看看这个代码朋友的

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString, options))
                    {
                        ArrayList myList = new ArrayList();
                        foreach (ListItem li in lstOutcome.Items)
                        {
                            myList.Add(li);
                        }
                        bulkCopy.DestinationTableName = "Customers";
                        //amount to bulkcopy at once to prevent slowing with large imports
                        bulkCopy.BatchSize = 200;
                        SqlBulkCopyColumnMapping map1 = new SqlBulkCopyColumnMapping(myList[0].ToString(), "CustomerID");
                        bulkCopy.ColumnMappings.Add(map1);
                        SqlBulkCopyColumnMapping map2 = new SqlBulkCopyColumnMapping(myList[1].ToString(), "Name");
                        bulkCopy.ColumnMappings.Add(map2);
                        SqlBulkCopyColumnMapping map3 = new SqlBulkCopyColumnMapping(myList[2].ToString(), "Address");
                        bulkCopy.ColumnMappings.Add(map3);
                        SqlBulkCopyColumnMapping map4 = new SqlBulkCopyColumnMapping(myList[3].ToString(), "City");
                        bulkCopy.ColumnMappings.Add(map4);
                        SqlBulkCopyColumnMapping map5 = new SqlBulkCopyColumnMapping(myList[4].ToString(), "State");
                        bulkCopy.ColumnMappings.Add(map5);
                        SqlBulkCopyColumnMapping map6 = new SqlBulkCopyColumnMapping(myList[5].ToString(), "ZipCode");
                        bulkCopy.ColumnMappings.Add(map6);
                        SqlBulkCopyColumnMapping map7 = new SqlBulkCopyColumnMapping(myList[6].ToString(), "Phone");
                        bulkCopy.ColumnMappings.Add(map7);
                        SqlBulkCopyColumnMapping map8 = new SqlBulkCopyColumnMapping(myList[7].ToString(), "Email");
                        bulkCopy.ColumnMappings.Add(map8);

回答by SLaks

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])
        );
    }
}