[英]listBox not displaying desired results from a dataSource with C#
My code below is displaying this when run: 我的以下代码在运行时显示此代码:
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? 如何获取显示以下各行字段的信息?
Based on your requirements, I think this will do it. 根据您的要求,我认为这可以做到。
. 。
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);
}
}
}
Arrays aren't great to work with when using DataSources. 使用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: 然后尝试使用BindingSource的PositionChanged事件为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];
}
}
Here string[] Line = sr.ReadLine().Split(','); 此处string [] Line = sr.ReadLine()。Split(','); lines.Add(Line);
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++;
}
您的列表框仅显示对象。将其绑定到DisplayMemberPath属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.