2017-07-28 71 views
0

我真的很陌生,所以我很努力地直接爲我的問題得到答案。 我試圖在很多方面(YouTube,堆棧溢出,谷歌)找到我的問題的答案,但無法讓我的程序正常運行。C#DataGridView定義的表格數據從文件接收數據

我需要的程序是從m3u文件中獲取一個值到數據表的相應單元格中,而不是讀取並添加絕對一切。

我在網上找到的主要是如何讀取文本/ csv/excel和從文件本身導入所有數據,這不是我真正需要的或者我不明白如何實現我的使用的代碼,像那個問題:Reading from .txt file, then exporting data to DataGridView

我已經定義了應該從m3u文件中「吸取」數據的單元。

文件m3u文件結構爲:

#EXTINF:-1 TVG-ID = 「」 TVG-NAME = 「====例1 ====」 TVG-標誌= 「」 基團-title =「」,====示例1 ====
thestreamingsource1.com
#EXTINF:-1 tvg-ID =「」tvg-name =「==== Example2 ====」tvg- logo =「」group-title =「」,====例子2 ====
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

我需要的程序,只得到了價值結構如下: TVG-ID(這沒關係,如果它是空的)。 tvg-name。 tvg-logo(沒關係,如果它是空的)。 組標題。

到目前爲止,我有字符串讀取文件和數據網格的所有內容,準備接受數據。

形式背後的代碼是:

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

而且形式看起來像這樣: Read M3u UI

我很抱歉,如果這個問題已經回答了,但我一直沒找到解。

很高興能幫到您。

謝謝。

+0

請提供一個最小的工作示例,並改寫與你的問題。並且請直接在問題中提供所有信息(應回答問題時不需要點擊鏈接)。 – Jan

+0

感謝她的病態工作 –

+0

[將一個.txt文件放入DataGridView]可能的重複(https://stackoverflow.com/questions/7095359/putting-a-txt-file-into-a-datagridview) – Ramankingdom

回答

0

這應該讓你去:

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; } 
    } 
} 
相關問題