using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Testing2 { internal enum colHdrInfo { Payee, Type, Date, Amount, Source, Comment } public partial class Form1 : Form { private DataGridView dgv = new DataGridView(); private string titleFormat = @"Accounts from {0:d} to {1:d}"; public Form1() { InitializeComponent(); finishLayout(); } private readonly int[] colWidths = new int[] { 50, -1, -1, 10 }; private String[] allowedTypes; private void finishLayout() { createDataGridView(); IsMdiContainer = true; Text = "Accounts"; } private void createDataGridView() { dgv.RowHeaderMouseClick += Dgv_RowHeaderMouseClick; dgv.DataError += Dgv_DataError; dgv.BackgroundColor = Color.White; Font oldDefault = dgv.ColumnHeadersDefaultCellStyle.Font; dgv.ColumnHeadersDefaultCellStyle.Font = new Font(oldDefault, FontStyle.Bold); dgv.Dock = DockStyle.Fill; dgv.Name = "My CheckBook"; DataGridViewColumn dvgCol; foreach (colHdrInfo cHI in Enum.GetValues(typeof(colHdrInfo))) { dvgCol = !cHI.Equals(colHdrInfo.Type) ? createTextBox(cHI) : createComboBox(cHI); //dvgCol = createTextBox(cHI); dgv.Columns.Add(dvgCol); } dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; this.Controls.Add(dgv); } private void Dgv_DataError(object sender, DataGridViewDataErrorEventArgs e) { throw new NotImplementedException(); } // deletes row on right click on header. private void Dgv_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { String fileName = "C:\\Users\\Marc\\OneDrive\\Documents\\Financial\\Checkbook\\test.csv"; if (e.Button != MouseButtons.Right) return; populateGridView(fileName); } private AutoCompleteStringCollection _acsc = new AutoCompleteStringCollection(); private ComboBox _cbo; // this is a nasty trick to pass the combobox from one event handler to another private DataGridViewColumn createComboBox(colHdrInfo cHI) { allowedTypes = new string[] { "ABC,DEF,GHI,JKL,MNO,PQR" }; Array.Sort(allowedTypes); // _acsc.AddRange(allowedTypes); DataGridViewColumn blah = new DataGridViewComboBoxColumn() { HeaderText = cHI.ToString(), Name = cHI.ToString(), DataSource = allowedTypes, DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, SortMode = DataGridViewColumnSortMode.Programmatic }; return blah; } private DataGridViewColumn createTextBox(colHdrInfo cHI) { DataGridViewColumn blah = new DataGridViewTextBoxColumn(); return blah; } internal void populateGridView(String fileName) { StreamReader sr; try { sr = File.OpenText(fileName); } catch (System.IO.IOException e) { MessageBox.Show($"IO Error Reading {fileName}: {e.GetType().Name}", "Error", MessageBoxButtons.OK); return; } string line; // collect earliest and latest time values DateTime early = DateTime.MaxValue, late = DateTime.MinValue; while ((line = sr.ReadLine()) != null) { string[] vv = line.Split(','); if (vv.Length > Enum.GetValues(typeof(colHdrInfo)).Length) if (MessageBox.Show($"this line {line} is badly formed\nProceed?", "Bad Input", MessageBoxButtons.OKCancel) == DialogResult.Cancel) Application.Exit(); try { DataGridViewRow yydgv = new DataGridViewRow(); yydgv.CreateCells(dgv, vv); dgv.Rows.Add(yydgv); } catch (Exception exp) { if (!_acsc.Contains(vv[(int)colHdrInfo.Type])) if (MessageBox.Show($@"Bad Classification {vv[1]}\nDo you care?", "oh yeah", MessageBoxButtons.YesNo) == DialogResult.No) continue;// not quite rright else throw exp; } // end of reading text file sr.Close(); } } } }