我是视觉工作室的新手#我似乎无法添加文本文件(流阅读器)到我的第二列并添加年份。我尝试了一些东西,但这是我想出的。我试着做subitem add,但它对我不起作用。我只是按照我的老师和教科书的内容去做,但我没有多少运气。我唯一真正的问题是让流阅读器显示在第二列而不是第一列,因为我需要第一列添加年份。
修改。对不起,每当我尝试输入任何代码将其移动到列表视图中的第二列时,它会使txtfile消失或者根本不移动。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> Populationsstats = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
const int SIZE = 7;
const int COLS = 2;
string[] arrLines = new string[SIZE];
int[] values = new int[COLS];
double dblAverage;
double dbllargest;
double dblsmallets;
listView1.GridLines = true;
//listview default settings
listView1.View = View.Details;
listView1.FullRowSelect = true;
//colum header
listView1.Columns.Add("Year", 100);
listView1.Columns.Add("Increase", 120);
//declare a streamreader
//StreamReader inputFile;
StreamReader inputFile = File.OpenText("USPopulation.txt");
//open the file
// inputFile = File.OpenText("USPopulation.txt");
//populate list
while (!inputFile.EndOfStream)
{
Populationsstats.Add(inputFile.ReadLine());
// Populationsstats.Add(int.Parse(inputFile.ReadLine()));
}
//close file
inputFile.Close();
//show items on uspop txt
// listView1.Items.Clear();
dblAverage = 2384; //average equation
listBox1.Items.Add("The Annual Change In Population:" + dblAverage.ToString());
dbllargest = 1955; //largest value
listBox1.Items.Add("The Year with the Greatest Increase:" + dbllargest.ToString());
dblsmallets = 1967; //Smallest value
listBox1.Items.Add("The Year with the Least increase:" + dblsmallets.ToString());
// show items in list view columns
if (Populationsstats.Count != 0)
{
for (int index = 0; index < Populationsstats.Count; index++)
{
ListViewItem cols = new ListViewItem();
listView1.Items.Add(Populationsstats[COLS].ToString());
}
}
}
}
答案 0 :(得分:0)
也许尝试使用
string[] readText = File.ReadAllLines(path);
然后获取所需的行。