Advertisement
kijato

c#_csv_to_grid.cs

Dec 5th, 2021 (edited)
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. /*
  2. #!/usr/bin/mcs -r:System.Windows.Forms.dll -r:System.Drawing.dll
  3. set path=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\;%path%
  4. csc.exe -debug+ -target:winexe -r:System.Windows.Forms.dll -r:System.Drawing.dll c#_winforms_base.cs
  5. */
  6.  
  7. using System;
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10. using System.Data;
  11. using System.Collections.Generic; // for List()
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14.  
  15. using System.IO;
  16. //using System.Text.RegularExpressions;
  17.  
  18. public class Program
  19. {
  20.     static MenuStrip strip = new MenuStrip();
  21.     static StatusStrip statusStrip = new StatusStrip();
  22.     static ToolStripStatusLabel statusLabel = new ToolStripStatusLabel();
  23.     static ToolStripComboBox separator = new ToolStripComboBox();
  24.     static ToolStripTextBox filter = new ToolStripTextBox();
  25.     static DataGridView dataGridView = new DataGridView();
  26.    
  27.     [STAThread]
  28.     public static void Main()
  29.     {
  30.         try
  31.         {
  32.  
  33.         var f = new Form();
  34.             f.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
  35.             f.Size = new Size(500, 400);
  36.            
  37.             ToolStripMenuItem fileItem = new ToolStripMenuItem("&Fájl");
  38.             var openFileMenuItem = fileItem.DropDownItems.Add("&Megnyitás");
  39.                 openFileMenuItem.Click += new EventHandler (Open);
  40.             var saveFileMenuItem = fileItem.DropDownItems.Add("M&entés");
  41.                 saveFileMenuItem.Enabled = false;
  42.                 // username.Click += (s, e) => SomeTextBox.Text = "...";
  43.             var quitMenuItem = fileItem.DropDownItems.Add("&Kilépés");
  44.                 quitMenuItem.Click += new EventHandler ( Exit );
  45.             strip.Items.Add(fileItem);
  46.            
  47.             separator.Items.AddRange( new object[] { ",", ";"} );
  48.             separator.Text = ";";
  49.             separator.Width = 30;
  50.             strip.Items.AddRange( new ToolStripItem[] { separator });
  51.            
  52.             filter.Text = ".*";
  53.             strip.Items.AddRange( new ToolStripItem[] { filter });
  54.            
  55.             f.Controls.Add(strip);
  56.  
  57.             statusStrip.Items.AddRange(new ToolStripItem[] {statusLabel});
  58.             statusStrip.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
  59.             f.Controls.Add(statusStrip);
  60.  
  61.             dataGridView.Location = new Point(5, 30);
  62.             dataGridView.Size = new Size(f.Width-25, f.Height-95);
  63.             dataGridView.Anchor = ( AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right );
  64.             dataGridView.ReadOnly = true;
  65.             dataGridView.MultiSelect = true;
  66.             dataGridView.AllowUserToAddRows = false;
  67.             dataGridView.AutoResizeColumns();
  68.             dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  69.             f.Controls.Add(dataGridView);
  70.             //dataGridView.Dock = DockStyle.Fill; // Ez valószínűleg csak MainMenu tipus esetén működik.
  71.            
  72.             Application.Run(f);
  73.  
  74.         }
  75.         catch (Exception e)
  76.         {
  77.             statusLabel.Text = e.Message;
  78.         }
  79.     }
  80.  
  81.  
  82.     static private void Exit (object sender, EventArgs e)
  83.     {
  84.         Application.Exit();
  85.     }
  86.  
  87.     static private void Open (object sender, EventArgs e)
  88.     {
  89.         OpenFileDialog openDialog = new OpenFileDialog();
  90.         openDialog.Title = "Select A File";
  91.         openDialog.Filter = "Text Files (*.csv)|*.csv" + "|" +
  92.                             "All Files (*.*)|*.*";
  93.         openDialog.InitialDirectory = Environment.CurrentDirectory;
  94.         if ( openDialog.ShowDialog() == DialogResult.OK )
  95.         {
  96.             statusLabel.Text = openDialog.FileName;
  97.             try
  98.             {
  99.  
  100.                 //dataGridView.AutoGenerateColumns = false;
  101.                 dataGridView.DataSource = null;
  102.                 //dataTable.Clear();
  103.                 //dataTable.Reset();
  104.                 DataTable dataTable = new DataTable();
  105.                
  106.                 string s = "";
  107.                 ulong i = 0;
  108.                 var regexp = new Regex(filter.Text);
  109.                 using (StreamReader sr = new StreamReader(statusLabel.Text,Encoding.GetEncoding("Windows-1250"),false,64*1024*1024)) // .NET 5-től nem jó
  110.                 //using (StreamReader sr = new StreamReader(statusLabel.Text,Encoding.GetEncoding("UTF-8"),false,64*1024*1024)) //
  111.                 // using (StreamReader sr = new StreamReader(statusLabel.Text, Encoding.UTF8, false, 64 * 1024 * 1024)) // .NET5
  112.                 {
  113.                     s = String.Empty;
  114.                     while ((s = sr.ReadLine()) != null)
  115.                     {
  116.                         if ( i++ == 0 )
  117.                         {
  118.                             foreach ( var data in s.Split(separator.Text[0]) )
  119.                             {
  120.                                 dataTable.Columns.Add(data,typeof(string));
  121.                             }
  122.                         }
  123.                         else
  124.                         {
  125.                             if ( regexp.Match(s).Success )
  126.                                 dataTable.Rows.Add(s.Split(separator.Text[0]));
  127.                         }
  128.                         statusLabel.Text = i.ToString();
  129.                         statusStrip.Update();
  130.                     }
  131.                 }
  132.                    
  133.                 dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; // Enélkül folyamatosan újraszámol, igazítja a cellákat, ami sokszorosára növeli az időigényt.
  134.                 dataGridView.DataSource = dataTable;
  135.  
  136.                 foreach (DataGridViewRow dgrow in dataGridView.Rows)
  137.                 {
  138.                     dgrow.HeaderCell.Value = String.Format("{0}", dgrow.Index + 1);
  139.                     dgrow.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  140.                 }
  141.                 dataGridView.AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders );
  142.                 dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  143.                 //dataGridView.Update();
  144.                 //dataGridView.Refresh();
  145.                 statusLabel.Text = dataTable.Rows.Count.ToString();
  146.                 //DateTime.Now.ToString("h:mm:ss tt");
  147.  
  148.             }
  149.             catch (Exception e2)
  150.             {
  151.                 //System.Diagnostics.Debug.WriteLine(e2.Message);
  152.                 statusLabel.Text = e2.Message;
  153.                 statusStrip.Update();
  154.             }
  155.         }
  156.  
  157.        
  158.     }
  159.  
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement