using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; using LumenWorks.Framework.IO.Csv; using System.Xml; namespace TradeIdeas.TIQ { public partial class CsvPreview : UserControl { public static int colNameTally = 0; private string _contents = ""; public string SuggestedFileName { get; set; } public XmlNode windowXML = null; public void SetContents(string contents) { _contents = contents; UpdateContents(); } public void setPrototypeXML(XmlNode node) { windowXML = node; } public CsvPreview() { InitializeComponent(); } private void UpdateContents() { /*There seems to have been a slight performance enhancement when the data from the csvReader is first loaded to a DataTable; the DataTable then being bound to the datagridview (rather than direct loading of the csvreader data to the datagridview*/ try { dataGridView1.DataSource = null; dataGridView1.AllowUserToAddRows = true; dataGridView1.DataSource = getResultsTable(); /*Attempt to actually remove the last row, which is blank, threw an "Uncommitted new row cannot be deleted." exception. However, the below statement did the trick(after all rows have been populated). Note, please make sure that property is set to true before the new rows are created with data. */ dataGridView1.AllowUserToAddRows = false; //Set sort mode of columns to not sortable foreach (DataGridViewColumn col in dataGridView1.Columns) { col.SortMode = DataGridViewColumnSortMode.NotSortable; } dataGridView1.AllowUserToOrderColumns = true; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error!"); } } public DataTable getResultsTable() { DataTable dat = new DataTable(); TextReader textReader = new StringReader(_contents); CsvReader csvRead = new CsvReader(textReader, false); int columnCount = csvRead.FieldCount; int rowTally = 0; //Set up the columns... for (int i = 0; i < columnCount; i++) { dat.Columns.Add(); } //now add rows; while (csvRead.ReadNextRecord()) { if (rowTally >0) { dat.Rows.Add(); } for (int i = 0; i < columnCount; i++) { if (rowTally == 0) //The very first row data that comes off are the column names { //do-while prevent exception being thrown with emptystring for column, does a bit of //auto-number for a blank bu using a static global "colNameTally" string test = csvRead[i].ToString(); do { if (test == "") { test = "noname" + colNameTally.ToString(); } if (dat.Columns.Contains(test)) { colNameTally++; test = "noname" + colNameTally.ToString(); } } while (dat.Columns.Contains(test)); dat.Columns[i].ColumnName = test; } else { if (null == csvRead[i]) { dat.Rows[rowTally - 1][i] = ""; } else { dat.Rows[rowTally - 1][i] = csvRead[i].ToString(); } } } rowTally++; } return dat; } public void CreateAsWindow() { Form f = new Form(); string symbol = SuggestedFileName.Replace(".csv", ""); f.Text = "Results for " + symbol; this.Parent = f; this.Dock = DockStyle.Fill; f.Show(); } private void saveAsCsvButton_Click(object sender, EventArgs e) { using (SaveFileDialog dialog = new SaveFileDialog()) { dialog.FileName = SuggestedFileName; dialog.Filter = "CSV file|*.CSV|All files|*.*"; dialog.DefaultExt = "*.csv"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) System.IO.File.WriteAllText(dialog.FileName, _contents); } } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (windowXML != null) { //get symbol column index: int index = getSymbolColumnIndex(); String symbol = dataGridView1[index, dataGridView1.CurrentCell.RowIndex].Value.ToString(); TIQChart tiqChart = new TIQChart(); tiqChart.getPrototypeEditor().GetContents(windowXML); //taking the prototype settings from EoCAlert and populating the prototypeditor of the new TIQChart object tiqChart.setStockSymbol(symbol); //set symbol; int selectedIndex = getSelectedIndex(symbol, e.RowIndex); //"selected index" is relative to the contiguous block of rows of the symbol in question-not to the whole datagrid if (selectedIndex != -1) { tiqChart.produceChart(selectedIndex); tiqChart.Show(); } } } private int getSymbolColumnIndex() { int index = 0; foreach (DataGridViewColumn column in dataGridView1.Columns) { DataGridViewColumnHeaderCell headerCell = column.HeaderCell; string headerCaptionText = column.HeaderText; if(headerCaptionText.Contains("symbol")) { break; } index++; } return index; } private bool duplicateNamePresent(DataTable table, string colName) { foreach (DataColumn col in table.Columns) { if (col.ColumnName == colName) { return true; } } return false; } private int getSelectedIndex(string symbol,int selectedRowIndex) { int rowTally = 0; try { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["symbol"] != null && row.Cells["symbol"].Value.ToString() == symbol) { if (row.Index == selectedRowIndex) { break; } rowTally++; } } } catch { rowTally = -1; } return rowTally; } } }