4

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

user980150
  • 186
  • 2
  • 4
  • 12

7 Answers7

5

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;
    }
WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • @wpf-it-- unable to find CurrentCell property for Datagrid. – Naila Akbar Feb 07 '15 at 20:17
  • 1
    I dont know why you cannot find it but you can alternatively use DataGridBeginningEditEventArgs e object. It has e.Column and e.Row.DataContext properties to your benefit. In the GetCellValue() call, boundItem would now be e.Row.DataContext and cell.Column will be e.Column. – WPF-it Feb 11 '15 at 05:25
4

I found a different approach:

ContentPresenter cp = (ContentPresenter)e.Column.GetCellContent(e.Row);
YourDataType item = (YourDataType)cp.DataContext;
Gytis S
  • 159
  • 1
  • 8
2

Try this -

(e.EditingEventArgs.Source as TextBlock).Text
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • What's your column type? DataGridTextColumn or something else? If you used the above coed what issue you facing? – Rohit Vats Oct 13 '11 at 20:07
  • The `EditingEventArgs.Source` seems to be always `null` for me. – wondra Jan 08 '20 at 11:11
  • @RohitVats Would you have time for a suggestion/comment on this [post](https://stackoverflow.com/q/61688520/1232087) – nam May 08 '20 at 23:02
1
var row = e.Row.Item as DataRowView;
var value = row["ColumnName"];
//now you can do whatever you want with the value
Joseph
  • 1,458
  • 15
  • 20
0

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;
    }
Alfie
  • 1,903
  • 4
  • 21
  • 32
0
 private void dataGrid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGrid2[e.ColumnIndex, e.RowIndex].Value == null)
                {}
        }
Cornel
  • 4,652
  • 15
  • 48
  • 57
0

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.

Oliver
  • 43,366
  • 8
  • 94
  • 151