4

我在查找刚刚编辑的单元格的行和单元格索引时遇到问题DataGrid。我正在使用CellEditEnding事件来知道单元格何时被编辑。

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

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

3 回答 3

7

我已经让它工作了。这是它的样子:

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;
    }
于 2013-05-10T14:40:06.407 回答
0

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

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
                    }
                }
}
于 2014-09-01T06:45:53.630 回答
0

要获取没有“发件人”参数的行索引,您可以尝试:

 Convert.ToInt32(e.Row.Header)

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

 private void DataGridData_CellEditEnding(DataGridCellEditEndingEventArgs e)
    {
        int col1 = e.Column.DisplayIndex;
        int row1 = Convert.ToInt32(e.Row.Header);
    }
于 2017-08-07T20:35:56.067 回答