C# DataGridView defined table data that receives data from file -
i'm new coding i'm struggling answer question directly. tried find answer question on many ways (youtube, stack overflow, google) couldn't program run correctly.
what need program value m3u file appropriate cell on data table , not read , add absolutely everything.
what have found online how read text/csv/excel , import data file itself, not need or code not understand how implement use, question: reading .txt file, exporting data datagridview.
i have defined cells should "suck" data 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 need program following 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 have string reads content of file , data grid ready accept data.
the code behind form is:
public class thisclass { datagridview my_datagridview = new datagridview(); datatable my_datatable = new datatable(); // constructor , other methods in class, // not showed here... private void btnread_click(object sender, eventargs e) { // codes 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 form looks that:
i'm sorry if question answered couldn't find solution.
glad if help.
thanks.
this should 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 put on form bindinglist<mydatagridviewentry> mydatagridviewsource = new bindinglist<mydatagridviewentry>();//the bindinglist datagridview pull data public form1() { initializecomponent(); initializedatagridview();//set initial settings of datagridview } private void initializedatagridview() { my_datagridview.location = new point(this.location.x + 15, this.location.y + 15);//define place in form(you place 1 directly want using wysiwyg) this.controls.add(my_datagridview); my_datagridview.autosize = true; my_datagridview.autogeneratecolumns = true; my_datagridview.datasource = mydatagridviewsource;//link datagridview 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 file parsed } } private void filldatagridfromfile(string incomingfilepath) { //empty list mydatagridviewsource.clear();//you may or may not want this... don't know full requirements... //fill list using (streamreader sr = new streamreader(incomingfilepath)) { string currentline = string.empty; while ((currentline = sr.readline()) != null) { /*this not how write production code, * sake of example, format works know being done , why.*/ string[] splittedstring = currentline.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); string f1 = splittedstring.length > 0 ? splittedstring[0] : string.empty;//if splittedstring has more 0 entries, use entry[0], else use string.empty string f2 = splittedstring.length > 1 ? splittedstring[1] : string.empty;//if splittedstring has more 1 entries, use entry[1], else use string.empty string f3 = gettvgnamefromstring(splittedstring[0]);//extract text within string string f4 = splittedstring.length > 3 ? splittedstring[3] : string.empty;//if splittedstring has more 3 entries, use entry[3], else use string.empty /**/ //add entry 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 grouping regex find looking 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; } } }
Comments
Post a Comment