0

I have a C# program that I am reading a CSV file with. using StreamReader, I populate the combo box with the contents of the CSV. The CSV contains 4 fields. I only want to display the 1st value in the selected line. I then want to pass 2 of the other fields to another process (I'm trying to map a network printer by location). Here's the part where I read the 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);
}

This works perfectly. The only thing is the line is so long it's not readable. End users will just select the location, which is the 1st field.

for testing I used this code to display the result

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

Is there a way to display only the 1st field in the combo box but still use the index number passed from the combo box control? I was thinking of creating an array of just the 1st field but do not know how I could reference the array AND the combo box index.

Any help is mucho appreciado....

uvr
  • 515
  • 4
  • 12
  • Does this answer your question? [Parsing CSV files in C#, with header](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header) – Sinatr Apr 29 '21 at 13:13
  • That is very similar what I'm doing with the StreamReader. rbrettj's answer is more in line with what I want to do. – YankeeFanToo Apr 29 '21 at 17:38

2 Answers2

0

If I'm understanding your question correctly, it seems like you could just use string.split() to break your lines into their necessary fields for display, and then reference the selected index of the ComboBox as you're already doing.

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);
}
rbrettj
  • 36
  • 1
  • 8
  • That actually works like a charm. Thank you! I'm much closer - I can display the the value, but I now have to match that up to the printers in the other values in the CSV. Would it be easier/cleaner to re-stream the CSV once the location is selected and match that value with the split value and get the location that way? or is an array overcomplicating it? – YankeeFanToo Apr 29 '21 at 17:40
  • There are many, many ways to potentially solve your issue, but I cannot really offer much guidance as that is more of an opinion-based answer. If it were me, I'd probably read the CSV once and create an array or list of custom objects that held all of my values, and then get whatever info I needed using something like printers[selectedIndex].Location, etc. But that's getting outside the scope of your original question. – rbrettj Apr 29 '21 at 17:47
0

Actually, this worked for me with a little help from rbrettj. By splitting the values I was able to populate the combo box with what I needed. I also needed to be able to read the other values in the file, so a quick re-read of the file allowed me to match up the value from the first read and get what I needed.

This worked for me:

            // 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;
                }

            }

My array knowledge is spotty at best so re-reading the array and assigning values to the server locations will get me close enough.

Thank you all for your help ad guidance.