Performing mathematical operations on the data of CSV file

  • Thread starter Thread starter anonymous_3210
  • Start date Start date
A

anonymous_3210

Guest
Hi,

I have read a CSV file from the directory and now i need to perform certain math operations for the data of CSV.

1422848.png

I need to calculate: Cp as Val2-Val1/(6 std*dev) for each tests

Here is the code so far

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reading_CSV_file
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btOK.Enabled = false;
}

private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = ("comma seperated value | *.CSV");
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string strfilename = openFileDialog1.FileName;
if (File.Exists(strfilename))
{
textBox1.Text = strfilename;
}
else
{

strfilename = "";
}
if (!string.IsNullOrWhiteSpace(strfilename))
{
textBox1.Text = strfilename;
}
else
{
MessageBox.Show("Please select a file");
}
}
else
{
MessageBox.Show("Please select a file");
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

if (textBox1.Text == "")
btOK.Enabled = false;
else
btOK.Enabled = true;

}

private void Form1_Load(object sender, EventArgs e)
{

}

public static DataTable OpenCSV(string filePath)
{
DataTable dt = new DataTable();
FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

StreamReader sr = new StreamReader(fs);
string strLine = "";
string[] aryLine = null;
string[] tableHead = null;
int columnCount = 0;s
bool IsFirst = true;
while ((strLine = sr.ReadLine()) != null)
{
if (IsFirst == true)
{
tableHead = strLine.Split(',');
IsFirst = false;
columnCount = tableHead.Length;
for (int i = 0; i < columnCount; i++)
{
DataColumn dc = new DataColumn(tableHead);
dt.Columns.Add(dc);
}
}
else
{
aryLine = strLine.Split(',');
DataRow dr = dt.NewRow();
for (int j = 0; j < columnCount; j++)
{
dr[j] = aryLine[j];
}
dt.Rows.Add(dr);
}
}
if (aryLine != null && aryLine.Length > 0)
{
dt.DefaultView.Sort = tableHead[0] + " " + "asc";
}
sr.Close();
fs.Close();
return dt;
}

DataTable dt;

private void btOK_Click(object sender, EventArgs e)
{
dt = OpenCSV(textBox1.Text);
foreach (DataRow dr in dt.Rows)
{
// Get the first column
checkedListBox1.Items.Add(dr["Tests"]);
}
}

class Value
{
string testname;
string value1;
string value2;
public Value(string t, string v1, string v2)
{
testname = t;
value1 = v1;
value2 = v2;
}

public string Testname { get { return testname; } }
public string Value1 { get { return value1; } }
public string Value2 { get { return value2; } }
}


private void btGetValues_Click(object sender, EventArgs e)
{
//int checkedTest = checkedListBox1.SelectedIndex;
//MessageBox.Show(dt.Rows[checkedTest][1] + ", " + dt.Rows[checkedTest][2]);
List<Value> list = new List<Value>();

foreach (var i in checkedListBox1.CheckedItems)
{
int index = checkedListBox1.Items.IndexOf(i);
Value v = new Value(dt.Rows[index][0].ToString(), dt.Rows[index][1].ToString(), dt.Rows[index][2].ToString());
list.Add(v);
}

foreach (Value i in list)
{
MessageBox.Show(i.Testname + i.Value1 + i.Value2);
}
}


}
}



Akshay

Continue reading...
 
Back
Top