Answer on Question#38984-Programming, C#
1. The user shouldbe able to view data stored to the customer details file.Write the code that
Elina should use to read data to the customer details file and display it to
the user.
Solution.using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Windows.Forms;
namespace QCSharp
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(objectsender, EventArgs e)
{
// Readeach line of the file into a string array. Each element
// of thearray is one line of the file.
string[] lines=null;
try
{
lines = System.IO.File.ReadAllLines(@"Detailsfile.txt");//d:\Projects\QCSharp\QCSharp\bin\Debug\Detailsfile.txt
}
catch{ System.Console.WriteLine("File not found!"); }
//Add toColumns
char[]delimiters = new char[]{ '*' };
string[]words = lines[0].Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
foreach(string s inwords)
{
dataGridView1.Columns.Add(s.ToString(), s.ToString());
}
//Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");//Add to Rows
foreach(string line inlines)
{
words = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
dataGridView1.Rows.Add(words.ToArray());
}
}
}
}
Comments
Leave a comment