【问题标题】:How can i get the value of a datagrid cell on the dataGrid1_BeginningEdit Event?如何在 dataGrid1_BeginningEdit 事件中获取数据网格单元格的值?
【发布时间】:2011-10-13 15:54:02
【问题描述】:

当我使用 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;

非常感谢

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

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

        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 属性。
    • 我不知道您为什么找不到它,但您也可以使用 DataGridBeginningEditEventArgs e 对象。它具有对您有利的 e.Column 和 e.Row.DataContext 属性。在 GetCellValue() 调用中,boundItem 现在将是 e.Row.DataContext 并且 cell.Column 将是 e.Column。
    【解决方案2】:

    我发现了另一种方法:

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

    【讨论】:

      【解决方案3】:

      试试这个 -

      (e.EditingEventArgs.Source as TextBlock).Text
      

      【讨论】:

      • 您的列类型是什么? DataGridTextColumn 还是别的什么?如果您使用上述 coed,您面临什么问题?
      • EditingEventArgs.Source 对我来说似乎总是null
      • @RohitVats 您是否有时间对此post提出建议/评论
      【解决方案4】:
      var row = e.Row.Item as DataRowView;
      var value = row["ColumnName"];
      //now you can do whatever you want with the value
      

      【讨论】:

        【解决方案5】:
         private void dataGrid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
                {
                    if (dataGrid2[e.ColumnIndex, e.RowIndex].Value == null)
                        {}
                }
        

        【讨论】:

        • 抱歉,这是我拥有的 Datagrid,而不是 datagrid 视图。您知道如何为数据网格执行此操作吗?
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          这比其他一些有效的答案要简单得多:

              private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
              {
                  string content = (e.EditingEventArgs.Source as TextBlock).Text;
          
                  if (String.IsNullOrEmpty(content))
                      e.Cancel = true;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-09-21
            • 2019-12-31
            • 2021-07-08
            • 2014-09-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多