private void RemoveExcelColumns(ref DataTable dt) { try { GridColsCollection cols = columnData.VisibleColumns(); DataColumn col = null; //this has to be done in reverse order (RemoveAt) //OR by name(Remove) or it will have undesired result for (int i = dt.Columns.Count - 1; i >= 0; i--) { col = dt.Columns[i]; if (cols.ItemByColumnName(col.ColumnName) == null && col.ColumnName != "WorkAreaID" && col.ColumnName != " ") { dt.Columns.Remove(col); } } if (dt.Columns.Contains("A")) { dt.Columns.Remove("A"); } if (dt.Columns.Contains("X")) { dt.Columns.Remove("X"); } dt.AcceptChanges(); } catch (Exception ex) { LogUtility.LogException(ex); } }
public void UpdateForcedColumns() { for (int i = 0; i < _ForcedVisibleColumns.Length; i++) { try { _Columns.ItemByColumnName(_ForcedVisibleColumns[i]).Viewable = true; _Columns.ItemByColumnName(_ForcedVisibleColumns[i]).Visible = true; } catch (Exception ex) { } } for (int i = 0; i < _Columns.Count; i++) { bool canChange = false; for (int y = 0; y < _AvailableForcedColumns.Length; y++) { if (_Columns.Item(i).ColumnName == _AvailableForcedColumns[y]) { canChange = true; break; // TODO: might not be correct. Was : Exit Do } } if (canChange == true) { bool columnExists = false; for (int y = 0; y < _ForcedVisibleColumns.Length; y++) { if (_Columns.Item(i).ColumnName == _ForcedVisibleColumns[y]) { columnExists = true; break; // TODO: might not be correct. Was : Exit Do } } if (columnExists == false) { _Columns.Item(i).Visible = false; } } } }
private void FormatExcelRows(ref DataTable dt) { GridColsCollection cols = columnData.VisibleColumns(); bool hasArchive = false; hasArchive = (cols.ItemByColumnName("ARCHIVE") != null); if (hasArchive) { dt.Columns.Add("ARCHIVE_NEW"); } int flag = 0; if (hasArchive) { foreach (DataRow row in dt.Rows) { if (hasArchive) { if (int.TryParse(row["ARCHIVE"].ToString(), out flag) && flag == 1) { row["ARCHIVE_NEW"] = "Yes"; } else { row["ARCHIVE_NEW"] = "No"; } } } dt.Columns["ARCHIVE_NEW"].SetOrdinal(dt.Columns["ARCHIVE"].Ordinal); dt.Columns.Remove("ARCHIVE"); dt.Columns["ARCHIVE_NEW"].ColumnName = "ARCHIVE"; } dt.Columns.Add(" "); dt.Columns.Add(" "); if (dt.Columns.Contains("WTS_SystemID")) { dt.Columns["WTS_SystemID"].SetOrdinal(dt.Columns.Count - 1); } dt.AcceptChanges(); }
private void RenameExcelColumns(ref DataTable dt) { GridColsCollection cols = columnData.VisibleColumns(); GridColsColumn gcCol = null; DataColumn col = null; for (int i = dt.Columns.Count - 1; i >= 0; i--) { col = dt.Columns[i]; gcCol = cols.ItemByColumnName(col.ColumnName); if (gcCol != null) { dt.Columns[col.ColumnName].ColumnName = gcCol.DisplayName; } } dt.AcceptChanges(); }