Microsoft Developer Network > Domovská stránka fór > Windows Presentation Foundation (WPF) > WPF DataGrid e.Row.Item in DataGridCellEditEndingEventArgs from the CellEditEnding event is null when casted to DataRowView.
Odeslat dotazOdeslat dotaz
 

OdpovědětWPF DataGrid e.Row.Item in DataGridCellEditEndingEventArgs from the CellEditEnding event is null when casted to DataRowView.

  • 28. října 2009 17:30terghost Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     

    I am trying to follow this is the example to commit changes with the WPF DataGrid on a cell by cell basis: http://www.scottlogic.co.uk/blog/wpf/2009/01/wpf-datagrid-committing-changes-cell-by-cell/


    The problem is that when I try it with my code, e.Row.Item in DataGridCellEditEndingEventArgs from the CellEditEnding event is null when I try to cast to DataRowView.  The difference between my code and the example is that I am binding with  LINQ2SQL like this:

    var
    result = from t in data.Topics
    select new { t };
    dataGrid.ItemsSource = result;


    This the XAML for the DataGrid:
    <dg:DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CellEditEnding="DataGrid_CellEditEnding" CurrentCellChanged="DataGrid_CurrentCellChanged">  
          <dg:DataGrid.Columns>          
                <dg:DataGridTextColumn Header="Topic" Width="150"  Binding="{Binding Path=t.TopicTitle}"/>
          </dg:DataGrid.Columns>
    </dg:DataGrid>


    These are the event handlers:
    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
       _dataRow = e.Row.Item as DataRowView;
    }

    private void DataGrid_CurrentCellChanged(object sender, EventArgs e)
    {
     if (_dataRow != null)
     {
        _dataRow.EndEdit();

        data.SubmitChanges();
        _dataRow = null;
     }
    }



    I am not sure what or where I have done something wrong.  Any help would be appreciated.

Odpovědi

  • 4. listopadu 2009 16:59Zhi-Xin YeMSFT, ModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi,

    I think the items in the DataGrid are not of DataRowView type, you can set use the following code to check whether the item you get is null, if it's not null, check its type.

    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
          if (e.Row.Item != null)
          {
                // Check the item type
                 Console.WriteLine(e.Row.Item.GetType().Name);

               // If the item is for example of MyClass type, you can do the casting as follows:
               // MyClass itemdata = e.Row.Item as MyClass;
           }
    }


    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin Ye


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!

Všechny reakce

  • 4. listopadu 2009 16:59Zhi-Xin YeMSFT, ModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    Hi,

    I think the items in the DataGrid are not of DataRowView type, you can set use the following code to check whether the item you get is null, if it's not null, check its type.

    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
          if (e.Row.Item != null)
          {
                // Check the item type
                 Console.WriteLine(e.Row.Item.GetType().Name);

               // If the item is for example of MyClass type, you can do the casting as follows:
               // MyClass itemdata = e.Row.Item as MyClass;
           }
    }


    If anything is unclear, please feel free to let me know.

    Best Regards,
    Zhi-Xin Ye


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!