Display contents of Textfile data to ListView in C

2019-08-20 17:23发布

问题:

I have a listview in my form.

In my text file I have this:


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; represents the time where I want to put in the first column of the listview

x;0.0078;y;-0.0147;z;0.9879; is where I want to create three columns to put the X,Y,Z in the each column and the data in the respective column

the next line will then be in row 2 in their respective column

they are separated by ";"

How I go about displaying it in the listview?

回答1:

Here is the new tested answer with the solution.

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();
        }

Here is the output(from the given data in question) :



回答2:

Try this

        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);
        }          

after this you can add your first data into listView and then do for remain the same. And make sure your listView view property set to Details.

output: