/// <summary> /// This method invokes open file dialog and updates DataGridView with /// table from selected CSV file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLoadData_Click(object sender, EventArgs e) { openFileDialog1.Filter = FILE_TYPE_FILTER; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { textFilePath.Text = openFileDialog1.FileName; } else { return; } dgvRockDataTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; dgvRockDataTable.AutoResizeColumns(); readCSV = new ReadWriteCSV(); dt = readCSV.BindDataCSV(textFilePath.Text); if (dt == null) { return; } dgvRockDataTable.DataSource = readCSV.BindDataCSV(textFilePath.Text); //Assigning current array of data to global variable to be accessed for statistics calculation currentheaders = readCSV.getAllColumnNames(); currentDataArray = readCSV.getOriginalDataArray(); populateStatisticsVariableList(currentheaders); }
/// <summary> /// This method generates new DataTable out of list of selected columns /// and original data (saved in instance of ReadWriteCSV class) by /// copying columns from original data set that match list of selected header names /// into new array. /// </summary> /// <param name="columnHeaders"></param> /// <returns></returns> private DataTable getNewDataTable(ArrayList columnHeaders) { string[,] originalDataArray = readCSV.getOriginalDataArray(); string[] originalheaders = readCSV.getAllColumnNames(); int newColumnCount = columnHeaders.Count; int newRowCount = originalDataArray.GetLength(0); string[,] newDataArray = new string [newRowCount, newColumnCount]; // This portion tranfers selected columns into new smaller string array. // Here code iterates over each new column and when selected header name mathches original data - // all rows get tranferred into new array. for (int i = 0; i < newColumnCount; i++) { for (int j = 0; j < originalheaders.Length; j++) { if (columnHeaders[i].Equals(originalheaders[j])) { for (int k = 0; k < newRowCount; k++) { newDataArray[k, i] = originalDataArray[k, j]; } } } } // this portion packs new string array into DataTable DataTable newdatatable = new DataTable(); if (newDataArray.Length > 0) { //first line to create header foreach (string headerWord in columnHeaders) { newdatatable.Columns.Add(new DataColumn(headerWord)); } //for data for (int r = 0; r < newRowCount; r++) { //splitting 2D array into series of arrays that contain only one array each string[] dataWords = new string[newColumnCount]; for (int k = 0; k < newColumnCount; k++) { dataWords[k] = newDataArray[r, k]; } //inserting array into DataRow DataRow dr = newdatatable.NewRow(); int columnIndex = 0; foreach (string headerWord in columnHeaders) { dr[headerWord] = dataWords[columnIndex++]; } newdatatable.Rows.Add(dr); } } //Assigning current array of data to global variable to be accessed for statistics calculation currentheaders = new string[newColumnCount]; int z = 0; foreach (string headerWord in columnHeaders) { currentheaders[z] = headerWord; z++; } currentDataArray = newDataArray; return(newdatatable); }