private void CreateDataTableColumns(DataTable dtData) { // Add in our column to store off the file line number. if (this.m_blnIncludeFileLineNumber && (this.m_intCreatedColumns < 1)) { dtData.Columns.Add(GenericParserAdapter.FILE_LINE_NUMBER); } for (int intColumnIndex = this.m_intCreatedColumns; intColumnIndex < this.m_lstColumnNames.Count; ++intColumnIndex, ++this.m_intCreatedColumns) { GenericParserAdapter.AddColumnToTable(dtData, this.m_lstColumnNames[intColumnIndex]); } }
/// <summary> /// Generates a <see cref="DataTable"/> based on the data stored within /// the entire data source after it was parsed. /// </summary> /// <returns> /// The <see cref="DataTable"/> containing all of the data in the data /// source. /// </returns> /// <exception cref="InvalidOperationException"> /// Attempting to read without properly setting up the <see cref="GenericParserAdapter"/>. /// </exception> /// <exception cref="ParsingException"> /// Thrown in the situations where the <see cref="GenericParserAdapter"/> cannot continue /// due to a conflict between the setup and the data being parsed. /// </exception> /// <example> /// <code lang="C#" escaped="true"> /// using (GenericParserAdapter p = new GenericParserAdapter(@"C:\MyData.txt")) /// DataTable dtResults = p.GetDataTable(); /// </code> /// </example> public DataTable GetDataTable() { DataRow drRow; DataTable dtData; int intCreatedColumns, intSkipRowsAtEnd; dtData = new DataTable(); dtData.BeginLoadData(); intCreatedColumns = 0; while (this.Read()) { // See if we have the appropriate number of columns. if (this.m_lstColumnNames.Count > intCreatedColumns) { // Add in our column to store off the file line number. if (this.m_blnIncludeFileLineNumber && (intCreatedColumns < 1)) { dtData.Columns.Add(GenericParserAdapter.FILE_LINE_NUMBER); } for (int intColumnIndex = intCreatedColumns; intColumnIndex < this.m_lstColumnNames.Count; ++intColumnIndex, ++intCreatedColumns) { GenericParserAdapter.AddColumnToTable(dtData, this.m_lstColumnNames[intColumnIndex]); } } if (!this.IsCurrentRowEmpty || !this.SkipEmptyRows) { drRow = dtData.NewRow(); if (this.m_blnIncludeFileLineNumber) { drRow[0] = this.FileRowNumber; // Now, add in the data retrieved from the current row. for (int intColumnIndex = 0; intColumnIndex < this.m_lstData.Count; ++intColumnIndex) { drRow[intColumnIndex + 1] = this.m_lstData[intColumnIndex]; } } else { // Since we don't have to account for the row number, just place the value right into the data row. drRow.ItemArray = this.m_lstData.ToArray(); } dtData.Rows.Add(drRow); } } intSkipRowsAtEnd = this.m_intSkipEndingDataRows; // Remove any rows at the end that need to be skipped. while ((intSkipRowsAtEnd-- > 0) && (dtData.Rows.Count > 0)) { dtData.Rows.RemoveAt(dtData.Rows.Count - 1); } dtData.EndLoadData(); return(dtData); }