JimDel
JimDel

Reputation: 4359

listBox not displaying desired results from a dataSource with C#

My code below is displaying this when run:

enter image description here

string filePath = @"C:\Users\Jim\Desktop\us-500.csv";
StreamReader sr = new StreamReader(filePath);
var lines = new List<string[]>();
int Row = 0;
while (!sr.EndOfStream)
{
    string[] Line = sr.ReadLine().Split(',');
    lines.Add(Line);
    Row++;
}

var data = lines.ToArray();

bindingSource1.DataSource = data;
listBox1.DataSource = bindingSource1;

How do i get it to show me each rows fields like below?

enter image description here

Upvotes: 0

Views: 829

Answers (4)

LarsTech
LarsTech

Reputation: 81610

Arrays aren't great to work with when using DataSources. Avoid converting your list into an array since it isn't necessary.

bindingSource1.DataSource = lines;
bindingNavigator1.BindingSource = bindingSource1;

Then try using the PositionChanged event of the BindingSource to set the DataSource for the ListBox:

void bindingSource1_PositionChanged(object sender, EventArgs e) {
  var items = bindingSource1.DataSource as List<string[]>;
  if (items != null && bindingSource1.Position >= 0 && 
                       bindingSource1.Position < items.Count ) {
    listBox1.DataSource = items[bindingSource1.Position];
  }
}

Upvotes: 1

user2107843
user2107843

Reputation: 259

Your List box shows only object.Bind it to DisplayMemberPath property.

Upvotes: 0

blaze_125
blaze_125

Reputation: 2317

Based on your requirements, I think this will do it.

  1. Read each line
  2. Split each line into multiple fields
  3. Add each field to DataSource
  4. Bind to DataSource

.

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace PopulateListBoxFromCSV_46815162
{

    public partial class Form1 : Form
    {
        ListBox lstbx = new ListBox();
        public Form1()
        {
            InitializeComponent();
            initializeListBox();
            //fillTheListBox(@"C:\temp\myfile.csv");
            fillTheListBox(@"C:\temp\myfile.csv", true);
        }

        /// <summary>
        /// Lets say you want to show field value only
        /// </summary>
        /// <param name="filePath"></param>
        private void fillTheListBox(string filePath)
        {

            List<string> results = new List<string>();
            string currentLine = string.Empty;
            using (StreamReader sr = new StreamReader(filePath))
            {
                while ((currentLine = sr.ReadLine()) != null)//for as long as there is something to read
                {
                    foreach (string item in currentLine.Split(','))//split the current line into multiple "fields"
                    {
                        results.Add(item);//add each individual field to the dataSource to create your 1FieldPerLine visual
                    }
                }
            }
            lstbx.DataSource = results;//bind your datasource
        }


        /// <summary>
        /// Lets say you wanted to show "Header" : "FieldValue"
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="ShowHeader"></param>
        private void fillTheListBox(string filePath, bool ShowHeader)
        {
            List<string> headerLine = new List<string>();
            List<string> results = new List<string>();
            int whichLineAmIon = 0;
            string currentLine = string.Empty;
            using (StreamReader sr = new StreamReader(filePath))
            {
                while ((currentLine = sr.ReadLine()) != null)//for as long as there is something to read
                {
                    string[] splitted = currentLine.Split(',');//split the line into fields
                    for (int i = 0; i < splitted.Length; i++)
                    {
                        if (whichLineAmIon == 0)//then we are on the header line
                        {
                            headerLine.Add(splitted[i]);
                        }
                        else
                        {
                            results.Add(headerLine[i] + " : " + splitted[i]);//add each individual field to the dataSource to create your 1FieldPerLine visual
                        }
                    }
                    whichLineAmIon++;
                }
            }

            lstbx.DataSource = results;//bind your datasource
        }

        private void initializeListBox()
        {
            lstbx.Dock = DockStyle.Fill;
            lstbx.BackColor = Color.Azure;
            this.Controls.Add(lstbx);
        }
    }
}

Upvotes: 1

Jorge Martins
Jorge Martins

Reputation: 1

Here string[] Line = sr.ReadLine().Split(','); lines.Add(Line);

You're add an array of strings instead a string.

For resolve this, you have to do other for, like this.

string[] Line = sr.ReadLine().Split(',');
for (int i = 0; i < Line.Length ; i++)
{
    lines.Add(Line[i]);
    Row++;
}

Upvotes: 0

Related Questions