Small issues with the WPF DataGrid
1) when using the WPF DataGrid, although the mode used when binding data is twoway,
However, in actual use, the transmission of data has a certain delay because the interface input is too fast.
This will sometimes cause exceptions. To solve this problem, we can call the function DataGrid. commiteedit ();
To manually transmit data.
2) some information, such as the number of rows, is not updated in time on the interface after the data is input, and after a row is deleted,
The number of rows is not rearranged. You only need to use repeated IR. Items. Refresh (); to refresh the interface.
3) Obtain a row or cell in the DataGrid
#region DataGrid Cell Row /// <summary> /// get the row of datagrid /// </summary> /// <param name="Index"></param> [0,n-1] /// <returns>DataGridRow</returns> private DataGridRow getRow(int Index) { try { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index); if (row == null) { dataGrid.UpdateLayout(); dataGrid.ScrollIntoView(dataGrid.Items[Index]); row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(Index); } return row; } catch (Exception ex) { log.Error("getRow()----- exception occurred," + ex.Message); return null; } } /// <summary> /// get the cell of dataGrid by an existing row /// </summary> /// <param name="rowIndex"></param> [0,n-1] /// <param name="columnIndex"></param> [0,n-1] /// <returns>DataGridCell</returns> private DataGridCell getCell(int rowIndex, int columnIndex) { try { DataGridRow rowContainer = getRow(rowIndex); if (rowContainer != null) { DataGridCellsPresenter presenter = getVisualChild<DataGridCellsPresenter>(rowContainer); if (presenter == null) { dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]); presenter = getVisualChild<DataGridCellsPresenter>(rowContainer); } DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); if (cell == null) { dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]); cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); } return cell; } // end of if (rowContainer != null) } catch (Exception ex) { log.Error("getCell()---- exception occurred, " + ex.Message); } return null; } /// <summary> /// get the visual child /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parent"></param> /// <returns></returns> private static T getVisualChild<T>(Visual parent) where T : Visual { try { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); child = v as T; if (child == null) { child = getVisualChild<T>(v); } if (child != null) { break; } } return child; } catch (Exception ex) { log.Error("getVisualChild()---- exception occurred, " + ex.Message); return null; } } #endregion