TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Farhan Shariff
NA
933
242.2k
Windows Form passing DataTable from one button to another
Mar 21 2014 9:18 AM
I want to compare two tables I am Importing two tables I want to compare them
how to pass the table from these buttons to another.
btBrowse= to browse the file
btupload = to upload csv file to datatable
Cmpbutton = I want to compare two Datatables uploaded
I want to pass table1 and table2 to
Cmpbutton_Click(object sender, EventArgs e)
{
DataTable table3 = CompareTwoDataTable(table1, table2);
}
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//
//
//LIMIT SHEET -- table1 Browse and upload
//
private void
btBrowse_Click
(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only csv files, we need to add this filter
openFileDialog.Filter = "|*.csv";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
tbCSVPath.Text = openFileDialog.FileName;
}
}
private void
btUpload
_Click
(object sender, EventArgs e)
{
try
{
DataTable table1 = GetDataTabletFromCSVFile(tbCSVPath.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Import CSV File", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable table1 = new DataTable("Limits");
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn1 = new DataColumn(column);
datecolumn1.AllowDBNull = true;
table1.Columns.Add(datecolumn1);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
table1.Rows.Add(fieldData);
}
DataRow[] rows1;
rows1 = table1.Select("SKIP = '1'");
foreach (DataRow r in rows1)
r.Delete();
//Real Sheet
table1.Columns.Add("point90", typeof(double));
table1.Columns.Add("point10", typeof(double));
table1.Columns.Add("Median", typeof(double));
//Refrence Sheet
table1.Columns.Add("point90_Refrence", typeof(double));
table1.Columns.Add("point10_Refrence", typeof(double));
table1.Columns.Add("Median_Refrence", typeof(double));
}
return table1;
}
//
//
//Real Sheet - table 2 Browse and upload
//
//
//
private void
btBrowse1_Click
(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only csv files, we need to add this filter
openFileDialog.Filter = "|*.csv";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
tbCSVPath1.Text = openFileDialog.FileName;
}
}
private void
btUpload1_Click
(object sender, EventArgs e)
{
try
{
DataTable table2 = GetDataTabletFromCSVFile1(tbCSVPath1.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Import CSV File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1)
{
DataTable table2 = new DataTable("Real");
using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
{
csvReader1.SetDelimiters(new string[] { "," });
csvReader1.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader1.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn2 = new DataColumn(column);
datecolumn2.AllowDBNull = true;
table2.Columns.Add(datecolumn2);
}
while (!csvReader1.EndOfData)
{
string[] fieldData1 = csvReader1.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData1.Length; i++)
{
if (fieldData1[i] == "")
{
fieldData1[i] = null;
}
}
table2.Rows.Add(fieldData1);
}
}
return table2;
}
// How to pass table1 and table 2 to this click event
private void
Cmpbutton_Click
(object sender, EventArgs e)
{
DataTable table3 = CompareTwoDataTable(table1, table2);
}
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{
string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}
for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = table2.Rows[j][col];
}
}
}
DataTable resultDt = table3.Clone();
for (int m = 0; m < table3.Columns.Count; m++)
{
DataView dv = new DataView(table3);
dv.Sort = filterExp.Split(',')[m];
table3 = dv.ToTable();
for (int i = 0; i < table3.Rows.Count; i++)
{
if (resultDt.Rows.Count != table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows[i][m] = table3.Rows[i][m];
}
}
return resultDt;
}
}
}
Reply
Answers (
2
)
Serial port breaks messages
Updating spreadsheet In Google Doc in C#