CrystalPerro
CrystalPerro

Reputation: 3

C# DataGridView defined table data that receives data from file

I'm really new to coding so I'm struggling to get an answer for my question directly. I tried to find answer for my question on many ways (YouTube, stack overflow, google) but couldn't get my program to run correctly.

What I need my program to do is get a value from an m3u file into the appropriate cell on my data table and not read and add absolutely everything.

What I have found online is mainly how to read text/csv/excel and import all the data from the file itself, this is not what I really need or code that i do not understand how to implement for my use, like that question: Reading from .txt file, then exporting data to DataGridView.

I have defined cells that should "suck" the data from the m3u file.

The file m3u file structure is:

#EXTINF:-1 tvg-ID="" tvg-name="==== Example1 ====" tvg-logo="" group-title="",==== Example1 ====
thestreamingsource1.com
#EXTINF:-1 tvg-ID="" tvg-name="==== Example2 ====" tvg-logo="" group-title="",==== Example2 ====
thestreamingsource2.com
#EXTINF:-1 tvg-ID="" tvg-name="==== Example3 ====" tvg-logo="" group-title="",==== Example3 ====
thestreamingsource3.com
#EXTINF:-1 tvg-ID="" tvg-name="==== Example4 ====" tvg-logo="" group-title="",==== Example4 ====
thestreamingsource4.com

And I need the program to only get the following from the value structure: tvg-ID (It's okay if it's empty). tvg-name. tvg-logo (It's okay if it's empty). group-title.

So far i have the string that reads all the content of the file and the data grid ready to accept data.

The code behind the form is:

public class ThisClass
{
    DataGridView my_datagridview = new DataGridView();
    DataTable my_datatable = new DataTable();

    // Constructor and other methods are in this class,
    // but not showed here...

    private void btnRead_Click(object sender, EventArgs e)
    {
        // Some codes are hidden here...

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string sFileName = openFileDialog1.FileName;               
            string[] alltext = File.ReadAllLines(sFileName);
            foreach (string text_line in alltext)
            {
               // MessageBox.Show(text_line);
            }
        }
    }
}

And the form looks like that: Read M3u UI

I'm sorry If the question is already answered but i couldn't find a solution.

Glad if you could help.

Thanks.

Upvotes: 0

Views: 350

Answers (1)

blaze_125
blaze_125

Reputation: 2317

This should get you going:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

using System.Text.RegularExpressions;

namespace DataGridView_45378237
{


    public partial class Form1 : Form
    {

        DataGridView my_datagridview = new DataGridView();//the DataGridView which will be put on the form
        BindingList<MyDatagridviewEntry> myDataGridviewSource = new BindingList<MyDatagridviewEntry>();//the BindingList from which the DataGridView will pull its data


        public Form1()
        {
            InitializeComponent();
            InitializeDataGridView();//set the initial settings of the DataGridView
        }

        private void InitializeDataGridView()
        {
            my_datagridview.Location = new Point(this.Location.X + 15, this.Location.Y + 15);//define where to place it in the form(you could obviously just place one directly where you want using the wysiwyg)
            this.Controls.Add(my_datagridview);

            my_datagridview.AutoSize = true;
            my_datagridview.AutoGenerateColumns = true;
            my_datagridview.DataSource = myDataGridviewSource;//link the DataGridView with the BindingSource

        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Browse Text Files";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "m3u";
            openFileDialog1.Filter = "All files (*.*)|*.*|m3u files (*.m3u)|*.m3u";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string sFileName = openFileDialog1.FileName;
                FillDataGridFromFile(sFileName);//send the file to get parsed
            }
        }

        private void FillDataGridFromFile(string incomingFilePath)
        {
            //empty the list
            myDataGridviewSource.Clear();//you may or may not want this... I don't know your full requirements...

            //fill the list
            using (StreamReader sr = new StreamReader(incomingFilePath))
            {
                string currentLine = string.Empty;

                while ((currentLine = sr.ReadLine()) != null)
                {
                    /*This is not how I would write the production code,
                     * but for the sake of this example, this format works well
                     so that you know what is being done and why.*/
                    string[] splittedString = currentLine.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string f1 = splittedString.Length > 0 ? splittedString[0] : string.Empty;//if splittedString has more than 0 entries, the use entry[0], else use string.empty
                    string f2 = splittedString.Length > 1 ? splittedString[1] : string.Empty;//if splittedString has more than 1 entries, the use entry[1], else use string.empty
                    string f3 = GetTVGNameFromString(splittedString[0]);//Extract the text from within the string
                    string f4 = splittedString.Length > 3 ? splittedString[3] : string.Empty;//if splittedString has more than 3 entries, the use entry[3], else use string.empty
                    /**/

                    //add the entry to the BindingSource
                    myDataGridviewSource.Add(new MyDatagridviewEntry { Col1 = f1, Col2 = f2, Col3 = f3, Col4 = f4 });
                }
            }
        }



        private string GetTVGNameFromString(string incomingString)
        {
            string retval = string.Empty;
            Regex rgx = new Regex("tvg-name=\"([^\"]*)\"");//use a grouping regex to find what you are looking for
            if (rgx.IsMatch(incomingString))
            {
                return rgx.Matches(incomingString)[0].Groups[1].Value;
            }
            return retval;
        }
    }



    public class MyDatagridviewEntry
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
    }
}

Upvotes: 0

Related Questions