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

发布于 2024-12-09 19:45:47 字数 528 浏览 0 评论 0原文

当我使用 dataGrid1_BeginningEdit 事件停止该事件时,我试图检查数据网格单元格的值是否为空。

代码如下,当我执行“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;

非常感谢

I'm trying to check if the value of a datagrid cell is null when i use the dataGrid1_BeginningEdit Event to stop the event.

code is as follows, i can use '(((TextBox)e.EditingElement).Text' when im doing 'dataGrid2_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)' but not for the below.

    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;

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤独患者 2024-12-16 19:45:47

我想这会对你有所帮助......

    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;
    }

I think this will help you....

    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;
    }
手心的海 2024-12-16 19:45:47

我发现了一种不同的方法:

ContentPresenter cp = (ContentPresenter)e.Column.GetCellContent(e.Row);
YourDataType item = (YourDataType)cp.DataContext;

I found a different approach:

ContentPresenter cp = (ContentPresenter)e.Column.GetCellContent(e.Row);
YourDataType item = (YourDataType)cp.DataContext;
紫瑟鸿黎 2024-12-16 19:45:47

试试这个 -

(e.EditingEventArgs.Source as TextBlock).Text

Try this -

(e.EditingEventArgs.Source as TextBlock).Text
天涯沦落人 2024-12-16 19:45:47
var row = e.Row.Item as DataRowView;
var value = row["ColumnName"];
//now you can do whatever you want with the value
var row = e.Row.Item as DataRowView;
var value = row["ColumnName"];
//now you can do whatever you want with the value
悲喜皆因你 2024-12-16 19:45:47
 private void dataGrid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGrid2[e.ColumnIndex, e.RowIndex].Value == null)
                {}
        }
 private void dataGrid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGrid2[e.ColumnIndex, e.RowIndex].Value == null)
                {}
        }
小兔几 2024-12-16 19:45:47

如果您查看 DataGridBeginningEditEventArgs,它们包含属性 ColumnRow,您可以使用它们从数据网格中选择单元格。

If you take a look at the DataGridBeginningEditEventArgs, they contain a property Column and Row which you can use to select the cell from the datagrid.

何以心动 2024-12-16 19:45:47

这比其他一些工作答案简单得多:

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

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

This is lot simpler then some of the other working answers:

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

        if (String.IsNullOrEmpty(content))
            e.Cancel = true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文