} // DataTableToGrid() public JObject DataTableToJSON(DataTable DT, bool Editable = false, string GroupByCol = "", CswEnumExtJsXType GroupByColType = null) { CswExtJsGrid grid = DataTableToGrid(DT, Editable, GroupByCol, GroupByColType); return(grid.ToJson()); } // DataTableToJSON()
} // _TreeNodeToGrid() public CswExtJsGrid DataTableToGrid(DataTable DT, bool Editable = false, string GroupByCol = "", CswEnumExtJsXType GroupByColType = null, bool IncludeEditFields = true, Dictionary <string, Type> ColumnTypeOverrides = null) { string NodeIdColName = "nodeid"; string MenuOptionsColName = "menuoptions"; string gridUniquePrefix = DT.TableName; CswExtJsGrid grid = new CswExtJsGrid(gridUniquePrefix, IncludeEditFields); grid.groupfield = GroupByCol; grid.title = DT.TableName; if (_CswNbtResources.CurrentNbtUser != null && _CswNbtResources.CurrentNbtUser.PageSize > 0) { grid.PageSize = _CswNbtResources.CurrentNbtUser.PageSize; } //CswExtJsGridDataIndex nodeIdDataIndex = new CswExtJsGridDataIndex( gridUniquePrefix, NodeIdColName ); //{ // CswExtJsGridField nodeIdFld = new CswExtJsGridField { dataIndex = nodeIdDataIndex }; // grid.fields.Add( nodeIdFld ); // CswExtJsGridColumn nodeIdCol = new CswExtJsGridColumn { header = "nodeId", dataIndex = nodeIdDataIndex, hidden = true }; // grid.columns.Add( nodeIdCol ); //} foreach (DataColumn CurrentColumn in DT.Columns) { Type CurrentColumnType = null; if (null != ColumnTypeOverrides) { if (false == ColumnTypeOverrides.ContainsKey(CurrentColumn.ColumnName)) { CurrentColumnType = CurrentColumn.DataType; } else { CurrentColumnType = ColumnTypeOverrides[CurrentColumn.ColumnName]; } } else { CurrentColumnType = CurrentColumn.DataType; } CswExtJsGridDataIndex dataIndex = new CswExtJsGridDataIndex(gridUniquePrefix, CurrentColumn.ColumnName); CswExtJsGridField fld = new CswExtJsGridField(); grid.fields.Add(fld); fld.dataIndex = dataIndex; CswExtJsGridColumn gridcol = new CswExtJsGridColumn(); gridcol.header = CurrentColumn.ColumnName; gridcol.dataIndex = dataIndex; gridcol.width = Math.Max(100, (7 + CurrentColumn.ColumnName.Length * 8)); if ((NodeIdColName.ToLower() == CurrentColumn.ColumnName.ToLower()) || (MenuOptionsColName.ToLower() == CurrentColumn.ColumnName.ToLower())) { gridcol.hidden = true; } if (CurrentColumnType == typeof(string)) { fld.type = "string"; } else if (CurrentColumnType == typeof(bool)) { fld.type = "bool"; gridcol.xtype = CswEnumExtJsXType.booleancolumn; } else if (CurrentColumnType == typeof(Int32) || (GroupByColType != null && CurrentColumn.ColumnName.ToLower().Equals(GroupByCol.ToLower()) && GroupByColType.Equals(CswEnumExtJsXType.numbercolumn))) { fld.type = "number"; gridcol.xtype = CswEnumExtJsXType.numbercolumn; gridcol.Format = "0"; } else if (CurrentColumnType == typeof(DateTime) || (GroupByColType != null && CurrentColumn.ColumnName.ToLower().Equals(GroupByCol.ToLower()) && GroupByColType.Equals(CswEnumExtJsXType.datecolumn))) { string userDateFormat = _CswNbtResources.CurrentNbtUser.DateFormat; string userTimeFormat = _CswNbtResources.CurrentNbtUser.TimeFormat; gridcol.dateformat = CswTools.ConvertNetToPHP(userDateFormat) + " " + CswTools.ConvertNetToPHP(userTimeFormat); fld.type = "date"; gridcol.xtype = CswEnumExtJsXType.datecolumn; gridcol.Format = "m/d/y H:i:s"; } else if (CurrentColumnType == typeof(sbyte)) //sbyte indidcates a "button" column :-( { gridcol.xtype = CswEnumExtJsXType.gridcolumn; gridcol.MenuDisabled = true; gridcol.IsSortable = false; } grid.columns.Add(gridcol); } Int32 RowNo = 0; foreach (DataRow Row in DT.Rows) { CswExtJsGridRow gridrow = new CswExtJsGridRow(RowNo, gridUniquePrefix); string NodeIdForGridRowData = string.Empty; foreach (DataColumn CurrentColumn in DT.Columns) { Type CurrentColumnType = null; if (null != ColumnTypeOverrides) { if (false == ColumnTypeOverrides.ContainsKey(CurrentColumn.ColumnName)) { CurrentColumnType = CurrentColumn.DataType; } else { CurrentColumnType = ColumnTypeOverrides[CurrentColumn.ColumnName]; } } else { CurrentColumnType = CurrentColumn.DataType; } CswExtJsGridDataIndex index = null; index = new CswExtJsGridDataIndex(gridUniquePrefix, CurrentColumn.ColumnName); gridrow.data[index] = CswConvert.ToString(Row[CurrentColumn]); if (Row.Table.Columns.Contains(NodeIdColName) && DBNull.Value != Row[NodeIdColName] && typeof(sbyte) == CurrentColumnType) { string MenuOptions = string.Empty; if (Row.Table.Columns.Contains(MenuOptionsColName)) { MenuOptions = Row[MenuOptionsColName].ToString(); } NodeIdForGridRowData = Row[NodeIdColName].ToString(); CswExtJsGridButton CurrentButton = new CswExtJsGridButton { DataIndex = index.ToString(), RowNo = RowNo, MenuOptions = MenuOptions, SelectedText = CurrentColumn.ColumnName, PropAttr = NodeIdForGridRowData }; //nu the button grid.rowData.btns.Add(CurrentButton); //add the button }//if it's the hi-jacked data tabe that means BUTTON }//iterate collumns grid.rowData.rows.Add(gridrow); RowNo += 1; } // foreach( DataRow Row in DT.Rows ) return(grid); } // DataTableToGrid()