【问题标题】:How can display only part of a line from a CSV file in a combo box?如何在组合框中仅显示 CSV 文件中的部分行?
【发布时间】:2021-04-29 12:56:24
【问题描述】:

我有一个正在读取 CSV 文件的 C# 程序。使用 StreamReader,我用 CSV 的内容填充组合框。 CSV 包含 4 个字段。我只想在所选行中显示第一个值。然后我想将其他字段中的 2 个传递给另一个进程(我正在尝试按位置映射网络打印机)。这是我阅读 CSV 的部分。

try
{
    // Open the CSV file to read
    StreamReader sr = new StreamReader(printerList);
    String currentline = sr.ReadLine();

    while (!sr.EndOfStream)
    {
        currentline = sr.ReadLine(); 
        comboBoxPrinterList.Items.Add(currentline);
    }
}
catch (Exception ex)
{
    // Display an error message if the file cannot be read
    MessageBox.Show("The file could not be read" + ex.Message);
}

这非常有效。唯一的问题是这条线太长了,不可读。最终用户只需选择位置,即第一个字段。

为了测试,我使用这段代码来显示结果

int selectedIndex = comboBoxPrinterList.SelectedIndex;
Object selectedItem = comboBoxPrinterList.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
                "Index: " + selectedIndex.ToString());

有没有办法只显示组合框中的第一个字段,但仍然使用从组合框控件传递的索引号?我正在考虑创建一个仅包含第一个字段的数组,但不知道如何引用该数组和组合框索引。

任何帮助都非常感谢....

【问题讨论】:

标签: c# combobox


【解决方案1】:

如果我正确理解您的问题,您似乎可以使用string.split() 将您的行分成必要的字段进行显示,然后像您已经在做的那样引用 ComboBox 的选定索引。

try
{
    // Open the CSV file to read
    StreamReader sr = new StreamReader(printerList);
    String currentline = sr.ReadLine();

    while (!sr.EndOfStream)
    {
        currentline = sr.ReadLine();
        
        // Break line into separated fields
        String[] fields = currentline.split(',');

        comboBoxPrinterList.Items.Add(fields[0]);
    }
}
catch (Exception ex)
{
    // Display an error message if the file cannot be read
    MessageBox.Show("The file could not be read" + ex.Message);
}

【讨论】:

  • 这实际上就像一个魅力。谢谢!我更接近了 - 我可以显示该值,但我现在必须将其与 CSV 中其他值中的打印机相匹配。一旦选择了位置并将该值与拆分值匹配并以这种方式获取位置,重新流式传输 CSV 会更容易/更干净吗?还是数组过于复杂?
  • 有很多很多方法可以潜在地解决您的问题,但我无法提供太多指导,因为这更多是基于意见的答案。如果是我,我可能会读取 CSV 一次并创建一个包含我所有值的自定义对象的数组或列表,然后使用诸如打印机 [selectedIndex].Location 等获取我需要的任何信息。但就是这样超出您最初问题的范围。
【解决方案2】:

实际上,在 rbrettj 的帮助下,这对我有用。通过拆分值,我能够用我需要的内容填充组合框。我还需要能够读取文件中的其他值,因此快速重新读取文件使我能够匹配第一次读取的值并获得所需的值。

这对我有用:

            // Open the CSV file to read
            StreamReader sr = new StreamReader(printerList);
            String currentline = sr.ReadLine();

            while (!sr.EndOfStream)
            {
                // Read each line of CSV file
                currentline = sr.ReadLine();

                // Break line into separated fields
                string[] fields = currentline.Split(',');

                string region = fields[0];
                string server1 = fields[2];
                string server2 = fields[3];

            if (officeLocation == region) { 
                    MessageBox.Show("Region: " + region + "\n" +
                        "Server1: " + server1 + "\n" +
                        "Server 2: " + server2);
                    break;
                }

            }

我的数组知识充其量是参差不齐的,因此重新读取数组并将值分配给服务器位置将使我足够接近。

感谢大家的帮助广告指导。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多