public DataGridCell GetCell(int row, int column, DataGrid dg) { try { DataGridRow rowContainer = GetRow(row, dg); if (rowContainer != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell == null) { dg.ScrollIntoView(rowContainer, dg.Columns[column]); cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); } return(cell); } else { return(null); } } catch (Exception exc) { //MessageBox.Show(exc.Message); return(null); } }
public static System.Windows.Controls.DataGridCell GetCell(System.Windows.Controls.DataGrid dataGrid, System.Windows.Controls.DataGridRow rowContainer, int column) { if (rowContainer != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = FindVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); if (presenter == null) { /* if the row has been virtualized away, call its ApplyTemplate() method * to build its visual tree in order for the DataGridCellsPresenter * and the DataGridCells to be created */ rowContainer.ApplyTemplate(); presenter = FindVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); } if (presenter != null) { System.Windows.Controls.DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as System.Windows.Controls.DataGridCell; if (cell == null) { /* bring the column into view * in case it has been virtualized away */ dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as System.Windows.Controls.DataGridCell; } return(cell); } } return(null); }
public DataGridCell GetCell(int row, int column, DataGrid dg) { try { DataGridRow rowContainer = GetRow(row, dg); if (rowContainer != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell == null) { dg.ScrollIntoView(rowContainer, dg.Columns[column]); cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); } return(cell); } return(null); } catch (Exception exc) { MessageBox.Show("Ошибка получения данных из таблицы функция (GetCell)!\n\n" + exc.Message, "Ошибка выполнения приложения", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } }
public static void ToPdf(DataGrid grid, string name) { Document document = new Document(PageSize.A4, 25, 25, 30, 30); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), name + ".pdf"), FileMode.Create)); document.Open(); PdfPTable table = new PdfPTable(grid.Columns.Count); foreach (DataGridColumn column in grid.Columns) { table.AddCell(new Phrase(column.Header.ToString())); } table.HeaderRows = 1; System.Collections.IEnumerable itemsSource = grid.ItemsSource as System.Collections.IEnumerable; if (itemsSource != null) { foreach (var item in itemsSource) { DataGridRow row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (row != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = FindVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(row); for (int i = 0; i < grid.Columns.Count; ++i) { DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(i); TextBlock txt = cell.Content as TextBlock; if (txt != null) { table.AddCell(new Phrase(txt.Text)); } } } } iTextSharp.text.Paragraph firstpara = new iTextSharp.text.Paragraph(name); document.Add(firstpara); document.Add(table); document.Close(); } }
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e) { DataGrid dataGrid = sender as DataGrid; if (dataGrid == null) { return; } if (e.Key == Key.Enter || e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left) { if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == dataGrid.Items.Count - 2) { var border = VisualTreeHelper.GetChild(dataGrid, 0) as Decorator; if (border != null) { var scroll = border.Child as ScrollViewer; if (scroll != null) { scroll.ScrollToEnd(); } } } } if (e.Key == Key.Enter) { TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); request.Wrapped = true; DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem); if (rowContainer != null) { int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn); System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); dataGrid.CommitEdit(); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.MoveFocus(request); dataGrid.SelectedItem = dataGrid.CurrentItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } else if (e.Key == Key.Down || e.Key == Key.Up || e.Key == Key.Right || e.Key == Key.Left) { int columnIndex = dataGrid.Columns.IndexOf(dataGrid.CurrentColumn); DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem); if (rowContainer != null) { System.Windows.Controls.Primitives.DataGridCellsPresenter presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell currentcell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); if (currentcell.Content is ContentPresenter) { DatePickerCus datepicker = FindVisualChild <DatePickerCus>(currentcell); if (datepicker != null && datepicker.IsDropDownOpen) { e.Handled = true; return; } ComboBoxWithSearch combo = FindVisualChild <ComboBoxWithSearch>(currentcell); if (combo != null && combo.IsDropDownOpen) { e.Handled = true; return; } } if (e.Key == Key.Down) { dataGrid.CommitEdit(); dataGrid.SelectedIndex++; rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem); if (rowContainer != null) { presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.Focus(); dataGrid.CurrentItem = dataGrid.SelectedItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } else if (e.Key == Key.Up) { if (dataGrid.ItemsSource != null && dataGrid.SelectedIndex == 0) { return; } dataGrid.CommitEdit(); dataGrid.SelectedIndex--; rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem); if (rowContainer != null) { presenter = GetVisualChild <System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer); DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); cell.Focus(); dataGrid.CurrentItem = dataGrid.SelectedItem; e.Handled = true; dataGrid.UpdateLayout(); dataGrid.BeginEdit(e); } } } } }