Step 1
Create an Empty Project -> Add Page
Step2
Add a Button and change the name to Browse .
Step3
Add a DataGridView Control Inside the Form -> Add a OpenFile Dialog in bottom of the Page.
Step4
Double click on Browse Button and go to the Code View.
Step5
Please follow the below code.
- private void button1_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
- {
- String sLine = "";
-
- try
- {
-
-
- System.IO.StreamReader FileStream = new System.IO.StreamReader(openFileDialog1.FileName);
-
-
-
- dataGridView1.AllowUserToAddRows = false;
-
-
- sLine = FileStream.ReadLine();
-
-
-
- string[] s = sLine.Split(';');
-
-
-
-
- for (int i = 0; i <= s.Count() - 1; i++)
- {
- DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
- colHold.Name = "col" + System.Convert.ToString(i);
- colHold.HeaderText = s[i].ToString();
- dataGridView1.Columns.Add(colHold);
- }
-
-
-
- sLine = FileStream.ReadLine();
-
- while (sLine != null)
- {
-
- dataGridView1.Rows.Add();
-
-
-
- for (int i = 0; i <= s.Count() - 1; i++)
- {
-
- s = sLine.Split(';');
-
- dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[i].Value = s[i].ToString();
- }
- sLine = FileStream.ReadLine();
- }
-
- FileStream.Close();
- }
- catch (Exception err)
- {
-
- System.Windows.Forms.MessageBox.Show("Error: " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- }
Step6
Once everything is finished, then click on Debug button and see the output .