如何在 dataGrid1_BeginningEdit 事件中获取数据网格单元格的值?

4
我正在尝试在使用dataGrid1_BeginningEdit事件停止事件时检查datagrid单元格的值是否为空。
以下是代码,我可以在执行“dataGrid2_CellEditEnding(object sender,DataGridCellEditEndingEventArgs e)”时使用“(((TextBox) e.EditingElement)。Text'”,但不能用于以下情况。
    private void dataGrid2_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        int column = dataGrid2.CurrentCell.Column.DisplayIndex;
        int row = dataGrid2.SelectedIndex;

        if (((TextBox)e.EditingElement).Text == null)
            return;

非常感谢

7个回答

5

我认为这将对您有所帮助....

    private void DataGrid_BeginningEdit(
        object sender,
        Microsoft.Windows.Controls.DataGridBeginningEditEventArgs e)
    {
        e.Cancel = GetCellValue(((DataGrid) sender).CurrentCell) == null;
    }

    private static object GetCellValue(DataGridCellInfo cell)
    {
        var boundItem = cell.Item;
        var binding = new Binding();
        if (cell.Column is DataGridTextColumn)
        {
            binding
              = ((DataGridTextColumn)cell.Column).Binding
                    as Binding;
        }
        else if (cell.Column is DataGridCheckBoxColumn)
        {
            binding
              = ((DataGridCheckBoxColumn)cell.Column).Binding
                    as Binding;
        }
        else if (cell.Column is DataGridComboBoxColumn)
        {
            binding
                = ((DataGridComboBoxColumn)cell.Column).SelectedValueBinding
                     as Binding;

            if (binding == null)
            {
                binding
                  = ((DataGridComboBoxColumn)cell.Column).SelectedItemBinding
                       as Binding;
            }
        }

        if (binding != null)
        {
            var propertyName = binding.Path.Path;
            var propInfo = boundItem.GetType().GetProperty(propertyName);
            return propInfo.GetValue(boundItem, new object[] {});
        }

        return null;
    }

@wpf-it-- 找不到Datagrid的CurrentCell属性。 - Naila Akbar
1
我不知道为什么你找不到它,但你可以使用DataGridBeginningEditEventArgs e对象作为替代。它有e.Column和e.Row.DataContext属性,可供您使用。在GetCellValue()调用中,boundItem现在将是e.Row.DataContext,而cell.Column将是e.Column。 - WPF-it

4
我找到了一种不同的方法:
ContentPresenter cp = (ContentPresenter)e.Column.GetCellContent(e.Row);
YourDataType item = (YourDataType)cp.DataContext;

2
尝试这个 -
(e.EditingEventArgs.Source as TextBlock).Text

你的列类型是什么?DataGridTextColumn还是其他类型?如果你使用了上述代码,你遇到了什么问题? - Rohit Vats
我的情况下,EditingEventArgs.Source 似乎总是 null - wondra
@RohitVats,您有时间对这个帖子提出建议或评论吗? - nam

1
var row = e.Row.Item as DataRowView;
var value = row["ColumnName"];
//now you can do whatever you want with the value

0
 private void dataGrid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGrid2[e.ColumnIndex, e.RowIndex].Value == null)
                {}
        }

抱歉,我手头的是 DataGrid 而不是 DataGridView。你知道如何针对 DataGrid 做到这一点吗? - user980150

0

这比其他一些可行的答案要简单得多:

    private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        string content = (e.EditingEventArgs.Source as TextBlock).Text;

        if (String.IsNullOrEmpty(content))
            e.Cancel = true;
    }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接