Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

multi collumn text file to listview c#

Hello HF!

I'm having some troubles, I have a text file which I would like to show organised on a big TV.

The text file is set like the following:
ID<>Name<TAB>Ordernumber<TAB>Reference

Now I would like to skip the ID because that's not important. The rest I would likt to show in a listview in C#

I'm not a coder and I can't seem to figure it out following an example I can find on google....

Does anyone have an example laying around that I can use and modify?
(Feb 26, 2019 12:47 AM)Mr.Magnificent Wrote:  Hello HF!

I'm having some troubles, I have a text file which I would like to show organised on a big TV.

The text file is set like the following:
ID<>Name<TAB>Ordernumber<TAB>Reference

Now I would like to skip the ID because that's not important. The rest I would likt to show in a listview in C#

I'm not a coder and I can't seem to figure it out following an example I can find on google....

Does anyone have an example laying around that I can use and modify?

Upload the original file,i would like to help you and solve the problem these are simple things.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Code
          string text_Path = "";
           OpenFileDialog OpenFileDialog = new OpenFileDialog();
           OpenFileDialog.Filter = "Text File (*.txt)|*.txt";
           if ((OpenFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK))
           {
               text_Path = OpenFileDialog.FileName;
           }


           int lineCount = File.ReadAllLines(text_Path).Length;
           using (System.IO.StreamReader objReader = new System.IO.StreamReader(text_Path))
           {



               for (int i = 0; i <= lineCount - 1; i++)
               {
                   string textline = objReader.ReadLine();
                   string[] spStr;

                   spStr = textline.Split('\t');

                   MessageBox.Show(spStr[1] + "," + spStr[2] + "," + spStr[3]);
                 //  listView1.Items.Add(spStr[1], spStr[2], spStr[3]);
               }

           }
[Image: GS8TPUR.png]

xaml:
Code
<Grid>
        <ListView x:Name="list" ItemsSource="{Binding}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="NAME" Width="Auto" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="ORDER NUMBER"  Width="Auto" DisplayMemberBinding="{Binding Path=Ordernumber}" />
                    <GridViewColumn Header="REFERENCE"  Width="Auto" DisplayMemberBinding="{Binding Path=Reference}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

Code
private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "Select  file";
            op.Filter = "Text files (Text files (*.txt)|*.txt";
            if (op.ShowDialog() == true)
            {
                list.DataContext = await Logic.DataManipulacion.Shared().LoadData(op.FileName);
            }
        }

Model
Code
public class DataModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Ordernumber { get; set; }
        public string Reference { get; set; }
    }

Logic:
Code
public class DataManipulacion
    {
        private char[] splitChar;
        private string[] dataSplit;
        private List<DataModel> collection;

        public async Task<List<DataModel>> LoadData(string _file)
        {
            using (var stream = new FileStream(_file, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (BufferedStream bs = new BufferedStream(stream))
            using (var reader = new StreamReader(bs))
            {
                collection = new List<DataModel>();
                string fileline;
                while ((fileline = await reader.ReadLineAsync()) != null)
                {
                    splitChar = new char[] { '\t' };
                    dataSplit = fileline.Split(splitChar);
                    if (dataSplit.Length > 1)
                    {
                        collection.Add(new DataModel()
                        {                            
                            Name=dataSplit[1],
                            Ordernumber=dataSplit[2],
                            Reference= dataSplit[3]
                        });
                    }
                    else
                    {
                        continue;
                    }

                }

                return collection;
            }
        }

        #region Singleton pattern

        private static DataManipulacion instanca;

        private DataManipulacion()
        {

        }

        public static DataManipulacion Shared()
        {
            if (instanca == null)
                instanca = new DataManipulacion();
            return instanca;
        }

        #endregion

    }
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Thanks very much! gonna try it right now :)

I'll let you know if i get it to work.


(Feb 26, 2019 03:08 AM)Mr.Magnificent Wrote:  Thanks very much! gonna try it right now :)

I'll let you know if i get it to work.

I have the file located in a folder already, is it possible to replace the filedialog with the file?

I can't seem to get the file dialog to open anyways :)
(Feb 26, 2019 03:08 AM)Mr.Magnificent Wrote:  Thanks very much! gonna try it right now :)

I'll let you know if i get it to work.



I have the file located in a folder already, is it possible to replace the filedialog with the file?

I can't seem to get the file dialog to open anyways :)

I just set as example to open file dialog when window is loaded (Loaded="Window_Loaded") and that part you need to add but you can remove file dialog and put a direct path to the file and in method down the file must be in the folder from which the application starts.

Code
private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var loc = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var filename = "data.txt";
            var fullpath = $@"{loc}\{filename}";            
            list.DataContext = await Logic.DataManipulacion.Shared().LoadData(fullpath);            
        }
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Feb 26, 2019 04:43 AM)itcorp Wrote:  I just set as example to open file dialog when window is loaded (Loaded="Window_Loaded") and that part you need to add but you can remove file dialog and put a direct path to the file and in method down the file must be in the folder from which the application starts.

Code
private async void Window_Loaded(object sender, RoutedEventArgs e)
       {
           var loc = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
           var filename = "data.txt";
           var fullpath = $@"{loc}\{filename}";            
           list.DataContext = await Logic.DataManipulacion.Shared().LoadData(fullpath);            
       }

Wow bud, thanks so much. I was really strugelling to get this to work.. If there's anything I can ever do for you just let me know :)
(Feb 26, 2019 06:29 AM)Mr.Magnificent Wrote:  Wow bud, thanks so much. I was really strugelling to get this to work.. If there's anything I can ever do for you just let me know :)

I'm glad you managed to solve it,thanks and all the best.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by pebblakOct 15, 2019 05:24 AM 6 308 Oct 15, 2019 05:24 AM
Last Post: Joseph Ducreux
[.NET] by NYANxCATJan 28, 2019 11:37 PM 9 499 Jan 28, 2019 11:37 PM
Last Post: SaketSaket
[Python] by streetlawkarmaJun 27, 2017 10:24 PM 4 186 Jun 27, 2017 10:24 PM
Last Post: streetlawkarma