public void DataBind(IAsyncProgress2 o) { ModuleProc PROC = new ModuleProc("", "DataBind"); _asyncActive = o; try { _pageIndex = 0; _pageTotal = 0; _totalRows = 0; o.CrossThreadInvoke(() => { dgvRows.ClearSelection(); dgvRows.Rows.Clear(); cboPageNos.Items.Clear(); cboPageNos.Enabled = false; cboPageNos.SelectedIndex = -1; tblButtons.Visible = false; this.EnableDisablePageButtons(); }); this.HasMessages = false; if (this.GridDataSource != null && this.GridDataSource.Count > 0) { o.CrossThreadInvoke(() => { tblButtons.Visible = true; }); this.HasMessages = true; _totalRows = this.GridDataSource.Count; _pageTotal = (_totalRows / this.RowsPerPage) + (((_totalRows % this.RowsPerPage) > 0) ? 1 : 0); for (int i = 1; i <= _pageTotal; i++) { o.CrossThreadInvoke(() => { cboPageNos.Items.Add(i); }); Thread.Sleep(1); } o.CrossThreadInvoke(() => { cboPageNos.Enabled = true; }); o.CrossThreadInvoke(() => { if (cboPageNos.Items.Count > 0) cboPageNos.SelectedIndex = 0; else cboPageNos.SelectedIndex = -1; }); } else { o.CrossThreadInvoke(() => { cboPageNos.Items.Clear(); lblPageInfo.Text = string.Empty; cboPageNos.SelectedIndex = -1; }); } } catch (Exception ex) { Log.Exception(PROC, ex); } }
public bool Write <T>(ExternalExcelWriteArgs <T> args) { ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "Write"); bool result = default(bool); try { IAsyncProgress2 o = args.Progress; object source = args.Source; bool writeHeaders = args.WriteHeaders; bool writeCheckItemsOnly = args.WriteCheckedItems; Action <ExternalExcelRangeFormatInfo> modifyRange = args.FormatInfo; Type sourceType = source.GetType(); bool isList = false; if (args.ExternalWrite != null) // It is caller's responsibility to do the write operation { return(args.ExternalWrite(this, args)); } if (source is DataTable) // Data Table { DataTable dt = source as DataTable; result = this.Write <DataColumn, DataRow>(o, dt, writeHeaders, dt.Columns, (c, i) => { return(c.ColumnName); }, dt.Rows, (r, i, c, j) => { return(r[c]); }, modifyRange); } else if (source is DataGridView) // Windows Forms Data Grid { DataGridView dgv = source as DataGridView; result = this.Write <DataGridViewColumn, DataGridViewRow>(o, dgv, writeHeaders, dgv.Columns, (c, i) => { return(c.HeaderText); }, dgv.Rows, (r, i, c, j) => { return(r.Cells[c.Index].Value); }, modifyRange); } else if (source is ListView) // List View { ListView lvw = source as ListView; ICollection items = lvw.Items; if (writeCheckItemsOnly) { o.CrossThreadInvoke(new Action(() => { if (lvw.MultiSelect && lvw.SelectedItems != null && lvw.SelectedItems.Count > 0) { items = lvw.SelectedItems; } else if (lvw.CheckBoxes && lvw.CheckedItems != null && lvw.CheckedItems.Count > 0) { items = lvw.CheckedItems; } })); } result = this.Write <ColumnHeader, ListViewItem>(o, lvw, writeHeaders, lvw.Columns, (c, i) => { return(c.Text); }, items, (r, i, c, j) => { return(r.SubItems[c.Index].Text); }, modifyRange); } else if (source is System.Windows.Controls.ItemsControl) // WPF Items Control { System.Windows.Controls.ItemsControl ctlItems = source as System.Windows.Controls.ItemsControl; System.Windows.Controls.DataGrid dgv = source as System.Windows.Controls.DataGrid; if (ctlItems.ItemsSource != null && typeof(IList).IsAssignableFrom(ctlItems.ItemsSource.GetType())) { source = ctlItems.ItemsSource; sourceType = source.GetType(); isList = true; } else if (dgv != null) { result = this.Write <System.Windows.Controls.DataGridColumn, System.Windows.Controls.DataGridRow>(o, dgv, writeHeaders, dgv.Columns, (c, i) => { return(c.Header); }, dgv.Items, (r, i, c, j) => { return(null); }, modifyRange); } } else if (typeof(IList).IsAssignableFrom(sourceType)) // IList (or) IList<T> { isList = true; } // if the item is derived from IList if (isList) { Type currentType = sourceType; Type typeOfT = null; while (currentType != typeof(object)) { Type[] types = currentType.GetGenericArguments(); if (types != null && types.Length > 0) { typeOfT = types[0]; break; } currentType = currentType.BaseType; } if (typeOfT == null) { return(false); } if (typeof(IList <T>).IsAssignableFrom(sourceType)) { this.WriteList <T>(source, typeOfT, ref result, o, writeHeaders, modifyRange); } else { this.WriteList <object>(source, typeOfT, ref result, o, writeHeaders, modifyRange); } } } catch (Exception ex) { Log.Exception(PROC, ex); } return(result); }
public bool Write <C, R>(IAsyncProgress2 o, object source, bool writeHeaders, IEnumerable columns, GetExcelWriteColumnName <C> getColumnName, IEnumerable items, GetExcelWriteRowItemValue <C, R> getItemValue, Action <ExternalExcelRangeFormatInfo> modifyRange) { ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "Write"); bool result = default(bool); if (source == null || items == null || columns == null) { return(false); } try { int rows = 0; int cols = 0; o.CrossThreadInvoke(new Action(() => { rows = (writeHeaders ? items.GetCollectionCount <R>() + 1 : items.GetCollectionCount <R>()); cols = columns.GetCollectionCount <C>(); })); int rowIndex = 1, rowIndex0 = 0; int colIndex = 1, colIndex0 = 0; object[,] values = Extensions.CreateArray(rows, cols, 1, 1); if (o != null) { o.InitializeProgress(1, rows); } ExternalExcelRangeFormatInfo rangeInfo = new ExternalExcelRangeFormatInfo(1, 1, rows, cols); if (modifyRange != null) { modifyRange(rangeInfo); } if (writeHeaders) { if (o != null) { o.UpdateStatusProgress(rowIndex, "Preparing row : " + rowIndex.ToString()); } colIndex0 = 0; foreach (C col in columns.OfType <C>()) { o.CrossThreadInvoke(new Action(() => { values[rowIndex, colIndex] = getColumnName(col, colIndex0); })); using (ExternalExcelRangeFormatInfo rangeInfo2 = new ExternalExcelRangeFormatInfo(rowIndex, colIndex, rowIndex, colIndex)) { rangeInfo2.BackColor = Color.Blue; rangeInfo2.ForeColor = Color.White; rangeInfo2.HorizontalAlignment = ExternalExcelCellHAlign.AlignLeft; rangeInfo2.VerticalAlignment = ExternalExcelCellVAlign.AlignCenter; this.FormatRange(rangeInfo2); } colIndex++; colIndex0++; } using (ExternalExcelRangeFormatInfo rangeInfo2 = new ExternalExcelRangeFormatInfo(rowIndex, 1, rowIndex, cols)) { rangeInfo2.AutoFilter = true; this.FormatRange(rangeInfo2); } rowIndex++; } IEnumerable items2 = null; o.CrossThreadInvoke(new Action(() => { items2 = items.OfType <R>(); })); IEnumerator enumerator = items2.GetEnumerator(); if (enumerator != null) { bool hasNext = true; while (hasNext) { o.CrossThreadInvoke(new Action(() => { hasNext = enumerator.MoveNext(); })); if (hasNext && enumerator.Current != null) { R dr = (R)enumerator.Current; if (rowIndex > rows) { break; } colIndex = 1; colIndex0 = 0; foreach (C col in columns) { o.CrossThreadInvoke(new Action(() => { values[rowIndex, colIndex] = getItemValue(dr, rowIndex0, col, colIndex0); })); colIndex++; colIndex0++; } if (o != null) { if (o.ExecutorService != null && o.ExecutorService.IsShutdown) { break; } o.UpdateStatusProgress(rowIndex, "Preparing row : " + rowIndex.ToString()); } rowIndex++; rowIndex0++; Thread.Sleep(1); } } } // excel writing result = this.Write(o, values, rangeInfo); // excel formatting o.UpdateStatus("Formatting..."); this.FormatRange(rangeInfo); } catch (Exception ex) { Log.Exception(PROC, ex); } return(result); }
public void DataBind(IAsyncProgress2 o) { ModuleProc PROC = new ModuleProc("", "DataBind"); _asyncActive = o; try { _pageIndex = 0; _pageTotal = 0; _totalRows = 0; o.CrossThreadInvoke(() => { dgvRows.ClearSelection(); dgvRows.Rows.Clear(); cboPageNos.Items.Clear(); cboPageNos.Enabled = false; cboPageNos.SelectedIndex = -1; tblButtons.Visible = false; this.EnableDisablePageButtons(); }); this.HasMessages = false; if (this.GridDataSource != null && this.GridDataSource.Count > 0) { o.CrossThreadInvoke(() => { tblButtons.Visible = true; }); this.HasMessages = true; _totalRows = this.GridDataSource.Count; _pageTotal = (_totalRows / this.RowsPerPage) + (((_totalRows % this.RowsPerPage) > 0) ? 1 : 0); for (int i = 1; i <= _pageTotal; i++) { o.CrossThreadInvoke(() => { cboPageNos.Items.Add(i); }); Thread.Sleep(1); } o.CrossThreadInvoke(() => { cboPageNos.Enabled = true; }); o.CrossThreadInvoke(() => { if (cboPageNos.Items.Count > 0) { cboPageNos.SelectedIndex = 0; } else { cboPageNos.SelectedIndex = -1; } }); } else { o.CrossThreadInvoke(() => { cboPageNos.Items.Clear(); lblPageInfo.Text = string.Empty; cboPageNos.SelectedIndex = -1; }); } } catch (Exception ex) { Log.Exception(PROC, ex); } }