Small issues with the WPF DataGrid

Source: Internet
Author: User
Tags wpf datagrid
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.