wpf 如何在DataGrid中获取刚刚编辑的单元格的行和单元格索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16484177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:46:06  来源:igfitidea点击:

How to get row's and cell's index of a just edited cell in DataGrid

c#wpfdatagrid

提问by Patryk

I'm having problem with finding row's and cell's index of a just edited cell in DataGrid. I'm using CellEditEndingevent to know when the cell has been edited.

我在DataGrid. 我正在使用CellEditEnding事件来了解单元格何时被编辑。

Till now I've managed to do something like this. Col1contains property DisplayIndexand that is index of selected column but I can't find in the same way.

到目前为止,我已经设法做到了这样的事情。Col1包含属性DisplayIndex,即所选列的索引,但我无法以相同的方式找到。

    private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        DataGridColumn col1 = e.Column;
        DataGridRow row1 = e.Row;
    }

回答by Patryk

I've got it working now. This is how it looks like:

我现在已经开始工作了。这是它的样子:

private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        DataGridColumn col1 = e.Column;
        DataGridRow row1 = e.Row;
        int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
        int col_index = col1.DisplayIndex;
    }

回答by zen_1991

Late answer but for anyone who finds this question this code will work by adding this event to your datagrid:

迟到的答案,但对于发现此问题的任何人,此代码都可以通过将此事件添加到您的数据网格来工作:

private void dgMAQ_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
DependencyObject dep = (DependencyObject)e.EditingElement;
            string newText = string.Empty;

        //Check if the item being edited is a textbox
        if (dep is TextBox)
        {
            TextBox txt = dep as TextBox;
            if (txt.Text != "")
            {
                newText = txt.Text;
            }
            else
            {
                newText = string.Empty;
            }
        }
        //New text is the new text that has been entered into the cell

       //Check that the value is what you want it to be
        double isDouble = 0;
        if (double.TryParse(newText, out isDouble) == true)
        {

            while ((dep != null) && !(dep is DataGridCell))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;


            if (dep is DataGridCell)
            {
                DataGridCell cell = dep as DataGridCell;

                // navigate further up the tree
                while ((dep != null) && !(dep is DataGridRow))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null)
                    return;

                DataGridRow row = dep as DataGridRow;
                int rowIndex = row.GetIndex();

                int columnIndex = cell.Column.DisplayIndex;

        //Check the column index. Possibly different save options for different columns
                if (columnIndex == 3)
                {

                    if (newText != string.Empty)
                    {
                        //Do what you want with newtext
                    }
                }
}

回答by Declan Taylor

To get the row-index without a 'sender' argument you might try:

要在没有“发送者”参数的情况下获取行索引,您可以尝试:

 Convert.ToInt32(e.Row.Header)

combined with an abbreviated form of Patryk's answer this gives:

结合 Patryk 答案的缩写形式,这给出了:

 private void DataGridData_CellEditEnding(DataGridCellEditEndingEventArgs e)
    {
        int col1 = e.Column.DisplayIndex;
        int row1 = Convert.ToInt32(e.Row.Header);
    }