Advertisement
solidsnake

C#FillingDataGridViewFromA.txtFile

May 19th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. FORM 1 -------------------------------------------------------------------------------------------------------
  2.  
  3.     public partial class Form1 : Form
  4.     {
  5.         private List<Song> l = new List<Song>();
  6.  
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.            
  11.             LoadSongs();
  12.             LoadTable();
  13.         }
  14.  
  15.         public void LoadSongs()
  16.         {
  17.             FileStream fs = new FileStream(@"C:\Users\Mikey\Desktop\?.txt", FileMode.Open);
  18.             StreamReader r= new StreamReader(fs);
  19.  
  20.             while (!r.EndOfStream)
  21.             {
  22.                 string line = r.ReadLine();
  23.                 string[] arr = line.Split('|');
  24.                 Song x = new Song( arr[0], arr[1], arr[2], double.Parse(arr[3]) );
  25.                 l.Add(x);
  26.             }
  27.  
  28.             r.Close();
  29.             fs.Close();
  30.         }
  31.  
  32.         public void LoadTable()
  33.         {
  34.             foreach (Song s in l)
  35.             {
  36.                 dataGridView1.Rows.Add(s.artist, s.album, s.genre, s.price);
  37.             }
  38.         }
  39.  
  40.  
  41.  
  42.         private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
  43.         {
  44.             //MessageBox.Show("You have selected row number" + dataGridView1.SelectedRows[0].Cells[0].Value + "");
  45.             //MessageBox.Show("You have selected row number " + dataGridView1.SelectedRows[0].Index);
  46.         }
  47.  
  48.       }
  49.  
  50. SONG -------------------------------------------------------------------------------------------------------
  51.  
  52.     class Song
  53.     {
  54.         public string artist, album, genre;
  55.         public double price;
  56.  
  57.         public Song(string a, string alb, string g, double p)
  58.         {
  59.             artist = a;
  60.             album = alb;
  61.             genre = g;
  62.             price = p;
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement