In: Computer Science
Your software development company wants to continue developing and enhancing a software application for internal use.
For this week's assignment, the application is required to read from a file (data.txt) containing employee data. Your tasks include:
Program Input File:
Using Visual Studio and C# programming concepts and using your Week one data.txt file as your source file, write a program to meet the specifications of the company's request. The program should have the following characteristics:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp1
{
public partial class frnChegProj1 : Form
{
public frnChegProj1()
{
InitializeComponent();
}
OpenFileDialog openFileDialog = new OpenFileDialog();
DataTable dt = new DataTable(); //As the datasource for Grid
int DrCurRow = 0;
bool header = true; //first time true
private void btnOpenFile_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog.ShowDialog() == DialogResult.OK)//Do some thing
only OK
{
if (openFileDialog.FileName.Contains("Data.txt"))//Do only file
name is the defined datafile
{
AssignDatatoGrid(openFileDialog.FileName);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void AssignDatatoGrid(string fileName)
{
using (TextReader txtreader = File.OpenText(fileName))
{
try
{
string linetxt;
while ((linetxt = txtreader.ReadLine()) != null)
{
string[] dataitems = linetxt.Trim().Split(',');
if (dataitems.Length > 6)
{
AddtoDtatatable(dataitems);
header = false;
}
}
}
catch(Exception ex)
{
throw ex;
}
}
dgvTxtSrc.DataSource = dt;
}
private void AddtoDtatatable(string[] CurRow)
{
try
{
if (header)
{
//add datatable header since predefined header creating
inadvance
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dt.Columns.Add(new DataColumn("Age", typeof(Int16)));
dt.Columns.Add(new DataColumn("Gross", typeof(double)));
dt.Columns.Add(new DataColumn("Department", typeof(Int16)));
dt.Columns.Add(new DataColumn("DevType", typeof(string)));
dt.Columns.Add(new DataColumn("TaxType", typeof(string)));
//add datatable rows
}
else
{
//add datatable rows
dt.Rows.Add();
for (int i = 0; i < CurRow.Length; i++)
{
dt.Rows[DrCurRow][i] = CurRow[i];
}
DrCurRow = DrCurRow + 1; //assgining /increasing row
}
}
catch(Exception ex)
{
throw ex;
}
}
}
}