【问题标题】:Display contents of Textfile data to ListView in C#在 C# 中将 Textfile 数据的内容显示到 ListView
【发布时间】:2017-07-24 06:48:57
【问题描述】:

我的表单中有一个列表视图。

在我的文本文件中,我有这个:


24-7-2017:13:44:40;x;0.0078;y;-0.0147;z;0.9879;

24-7-2017:13:44:41;x;0.0069;y;-0.0069;z;1.0104;


24-7-2017:13:44:40; 表示我要放入列表视图第一列的时间

x;0.0078;y;-0.0147;z;0.9879; 是我要创建三列以将 X、Y、Z 放在每一列中并将数据放在相应列中的位置

下一行将位于相应列的第 2 行

它们用“;”隔开

如何在列表视图中显示它?

【问题讨论】:

  • File.ReadAllLines 读取文件,用Split 分隔行,将它们保存在ObservableCollection 中并将此集合绑定到您的ListView。请分享您尝试过的内容以获取更多信息。
  • @azrin 这是 WPF 还是 WIndows 的哪种形式?

标签: c# forms listview text-files


【解决方案1】:

试试这个

        System.Windows.Forms.ListView listView = new System.Windows.Forms.ListView();
        DateTime = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        X = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        Y = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        Z = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
        SuspendLayout();

        listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        DateTime,
        X,
        Y,
        Z});
        listView.GridLines = true;
        listView.View = System.Windows.Forms.View.Details;
        DateTime.Text = "DateTime";
        X.Text = "X";
        Y.Text = "Y";
        Z.Text = "Z";
        this.Controls.Add(this.listView);

        StreamReader file = new StreamReader("filepath");

        string sLine;
        while ((sLine = file.ReadLine()) != null)
        {
            string[] sarr= sLine.Split(';');
            StringBuilder sb = new StringBuilder(sarr[0]);
            sb[sarr[0].IndexOf(':')] = ' ';
            sarr[0] = sb.ToString().Replace(':', '.');

            string[] sData = { sarr[0], sarr[2], sarr[4], sarr[6] };

            ListViewItem item = new ListViewItem(sData);

            listView.Items.Add(item);
        }          

在此之后,您可以将您的第一个数据添加到 listView 中,然后保持不变。并确保您的 listView 视图属性设置为 Details。

输出:

【讨论】:

  • OP 还要求在 ListView 中显示 3 个不同的列。您的回答没有解决这部分问题。
  • @MongZhu 现在好吗?
  • 这样只显示一行数据,x、y、z的值都不见了
  • 您的回答具有误导性,当仅采用您发布的代码时,OP 将永远不会得到您发布的图像所描绘的结果。列从哪里来?列的标题来自哪里? OP else(你没有提到)必须做什么才能达到这个结果?
【解决方案2】:

这是解决方案的新测试答案。

public Form1()
        {
            InitializeComponent();
            //read the file
            System.IO.StreamReader file =
                           new System.IO.StreamReader("yourFileName.txt");

            //set list view in details mode
            listView1.View = View.Details;

            //Set columns in listview
            listView1.Columns.Add("Date Time");
            listView1.Columns.Add("X");
            listView1.Columns.Add("Y");
            listView1.Columns.Add("Z");
            string line = "";
            //read text file line by line.     
            while (( line = file.ReadLine()) != null)
            {
                var itemMC = new ListViewItem(new[] { line.ToString().Split(';')[0].ToString(), line.ToString().Split(';')[2].ToString(), 
                    line.ToString().Split(';')[4].ToString(), line.ToString().Split(';')[6].ToString() });
                listView1.Items.Add(itemMC);

            }
            file.Close();
        }

这是输出(来自有问题的给定数据):

【讨论】:

  • 堆栈溢出在提交我的编辑时造成问题。这就是为什么,这需要时间。
  • @MongZhu,对了,我添加了一个listview项并添加到list view。现在它应该编译了。
  • 更新了答案,请检查现在有没有问题。这次我已经在我的机器上测试过了。
  • 现在您有了一个工作示例!即使有图像,很好的答案! +1
  • @AbdurRahim 嗨!您的回复对我来说意义重大!谢谢!很高兴!
猜你喜欢
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多