Example #1
0
        public static DataTable ReadFromCSV(string fullFilePath)
        {
            DataTable table = new DataTable();

            using (CSVReader reader = new CSVReader(fullFilePath))
            {
                // Create columns.
                ColumnName[] columns = new ColumnName[reader.TitlePositions.Count];
                int columnIndex = 0;

                foreach (KeyValuePair<string, int> titleInfo in reader.TitlePositions.OrderBy(p => p.Value))
                {
                    columns[columnIndex++] = table.AddColumn(titleInfo.Key);
                }

                // Iterate through each row in the CSV file and add the data to the table.
                for (int rowIndex = 0; rowIndex < reader.Length(); rowIndex++)
                {
                    // Create a new row.
                    Row row = table.AppendRow();
                    for(columnIndex = 0; columnIndex < columns.Length; columnIndex++)
                    {
                        ColumnName columnName = columns[columnIndex];
                        row[columnName] = reader.GetValue(rowIndex, columnName.Name);
                    }
                }
            }

            return table;
        }
Example #2
0
        public string this[ColumnName columnName]
        {
            get
            {
                if (_Table != columnName.Table)
                {
                    throw new ArgumentException("The specified column is not part of the current table.", "columnName");
                }

                string value;
                _Cells.TryGetValue(columnName, out value);
                return value;
            }
            set
            {
                if (_Table != columnName.Table)
                {
                    throw new ArgumentException("The specified column is not part of the current table.", "columnName");
                }

                _Cells[columnName] = value;
            }
        }