listBox 未使用 C# 显示来自数据源的所需结果
listBox not displaying desired results from a dataSource with C#
我下面的代码在 运行:
时显示这个
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;
我如何让它向我显示如下所示的每一行字段?
这里是string[] Line = sr.ReadLine().Split(',');
lines.Add(行);
您添加的是字符串数组而不是字符串。
要解决此问题,您必须执行其他操作,例如。
string[] Line = sr.ReadLine().Split(',');
for (int i = 0; i < Line.Length ; i++)
{
lines.Add(Line[i]);
Row++;
}
根据你的要求,我想这样就可以了。
- 阅读每一行
- 将每一行拆分为多个字段
- 将每个字段添加到数据源
- 绑定到数据源
.
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);
}
}
}
您的列表框仅 object.Bind 显示到 DisplayMemberPath 属性。
在使用数据源时,数组不太适合使用。避免将列表转换为数组,因为没有必要。
bindingSource1.DataSource = lines;
bindingNavigator1.BindingSource = bindingSource1;
然后尝试使用 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];
}
}
我下面的代码在 运行:
时显示这个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;
我如何让它向我显示如下所示的每一行字段?
这里是string[] Line = sr.ReadLine().Split(','); lines.Add(行);
您添加的是字符串数组而不是字符串。
要解决此问题,您必须执行其他操作,例如。
string[] Line = sr.ReadLine().Split(',');
for (int i = 0; i < Line.Length ; i++)
{
lines.Add(Line[i]);
Row++;
}
根据你的要求,我想这样就可以了。
- 阅读每一行
- 将每一行拆分为多个字段
- 将每个字段添加到数据源
- 绑定到数据源
.
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);
}
}
}
您的列表框仅 object.Bind 显示到 DisplayMemberPath 属性。
在使用数据源时,数组不太适合使用。避免将列表转换为数组,因为没有必要。
bindingSource1.DataSource = lines;
bindingNavigator1.BindingSource = bindingSource1;
然后尝试使用 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];
}
}